English 中文(简体)
有一个有效的办法是将一个表格的所有浏览量转到另一个表格,一个记录是在某个时候。
原标题:is there an efficient way of transfering all the rows of one table to another table, one record at a time

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

参考书是来源表的主要关键。 是否有办法检查插入的记录的终结,这样就不必开始并维持假装。

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_no an IDENTITY column, then the system generates the unique values for you, and handles concurrency too).

CREATE TRIGGER dbo.SourceTrg ON dbo.Source
AFTER INSERT
AS
BEGIN
    SET NOCOUNT ON;

    DECLARE @seq INT;
    SET @seq = COALESCE((SELECT MAX(seq_no) 
      FROM dbase.dbo.Destination WITH (TABLOCKX, HOLDLOCK)), 0);

    INSERT dbase.dbo.Destination(SEQ_NO, orderno, PRODUCT_ID, qty)
      SELECT @seq + ROW_NUMBER() OVER (ORDER BY refno),
        ordernumber, productid, quantity
      FROM inserted;
END
GO
问题回答

暂无回答




相关问题
Export tables from SQL Server to be imported to Oracle 10g

I m trying to export some tables from SQL Server 2005 and then create those tables and populate them in Oracle. I have about 10 tables, varying from 4 columns up to 25. I m not using any constraints/...

SQL server: Can NT accounts be mapped to SQL server accounts

In our database we have an SQL server account that has the correct roles to access some of the databases. We are now switching to windows authentication and I was wondering if we can create a NT user ...

SQL Server 2000, ADO 2.8, VB6

How to determine if a Transaction is active i.e. before issuing Begin Transaction I want to ensure that no previous transaction are open.. the platform is VB6, MS-SQL Server 2000 and ADO 2.8

热门标签