requirement is to transfer all inserted records to one table to another. i’ve used a trigger to do it. i’m looping through all the inserted records and inserting to new table one record at a time as i have to increment a sequence number in the destination table. but this loop considarably slow when number of inserted rows increase. is there a better way of doing this.
Declare @maxpk int, @count int, @seq int
set @maxpk=(select max(refno) from inserted )
set @count=(select count(1) from inserted)
set @seq=((select max(seq_no) from dbase.dbo.destination))
while @count>0
begin
set @seq=(select @seq+1)
insert into dbase.dbo.destination(orderno,SEQ_NO,PRODUCT_ID,qty)
select ordernumber,@seq,productid ,quantity
from inserted where refno=@maxpk
set @count=(select @count-1)
set @maxpk=(select top 1 refno from inserted where refno<@maxpk)
end
refno is primary key of source table. is there a way to check the end of inserted records so i don’t have to initialize and maintain a loop counter?
and can loop be executed for each record in inserted table so i don’t have to find the next record to insert by comparing the value of primary key.
using mssql 2005
This should handle concurrency ok but I really think you need to revisit the design (e.g. make
seq_noanIDENTITYcolumn, then the system generates the unique values for you, and handles concurrency too).