Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
rubs_65
Posting Yak Master
144 Posts |
Posted - 2004-09-21 : 14:18:51
|
| Hi,We have a table having identity column. Currently we insert into this table 1 row at a time and check @@identity and insert into second table new row using this identity value. Now we have to convert this in batch processing and have to insert 1000 rows at a time in first table. What is the best possible to check which are the new rows inserted by particular session and insert into second table? Note: We can't use triggersThanks--rubs |
|
|
DustinMichaels
Constraint Violating Yak Guru
464 Posts |
Posted - 2004-09-21 : 14:42:05
|
| oopsy i didn't read your post correctly. |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2004-09-21 : 14:53:14
|
| Declare @startId int, @endId intSelect @startId = max(id)+1 From table1/*******Do insert into table1 here*******/Select @endId = max(id) From table1Insert Into table2Select col01*2From table1Where id between @startId and @endIdCorey |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2004-09-21 : 17:01:34
|
| I take it you have the data in a staging table and want to put some of the row into a master table and the rest into a child?If you just need the identity value then Seventhnight's solution will work. OtherwiseNote - all of these only work in a single user environment - lock the table if necessarySimple solutionselect *, identity(int, 1, 1) as id into #a from stagingdeclare @id intselect @id = max(id) from tblset identity_insert tbl oninsert tbl select id + @id, col, ... from #ainsert tbl2 select id + @id, col, ... from #aset indetity_insert tbl offmore complicated - needed if all rows don't go into masterselect *, identity(int, 1, 1) as id, seq = 0 as id into #a from stagingpick any column colx that is as unique as possible - can use multiple fields - that are going into the master table.update #a set seq = (select count(*) from #a t2 where #a.colx = t2.colx and t2.id <= #a.id)declare @seq intselect @seq = max(seq) from #adeclare @id intselect @id = max(id) from tblwhile @seq > 0begininsert tbl select col, ... from #a where seq = @seqinsert tbl2 select tbl.id, #a.col, ... from tbl, #a where #a.colx = tbl.colx and #a.seq = @seq and tbl.id > @idselect @seq = @seq - 1endThe loop will execute on distinct values so the more copies of a single field the more times it wil execute.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
|
|
|
|
|