要求是将所有插入的记录转移到一个表到另一个表。我用触发器来做到这一点。我遍历所有插入的记录并一次插入新表一条记录,因为我必须增加目标表中的序列号。但是当插入的行数增加时,这个循环相当慢。有没有更好的方法来做到这一点。
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 是源表的主键。有没有办法检查插入记录的结尾,所以我不必初始化和维护循环计数器?
并且可以为插入表中的每条记录执行循环,因此我不必通过比较主键的值来查找要插入的下一条记录。使用 mssql 2005