我有一个带有20,000个条目的表。
我需要复制“某些”数据,但是要使用新的公司名称。
该表具有一个不会自动递增的唯一ID,因此在每次插入过程中,我需要找到MAX(UniqueID)并添加1。
以下脚本有效,但是性能很差。
DECLARE @RowCount AS INTEGER;
SELECT @RowCount = COUNT(1)
FROM [dbo].[TableAAA];
DECLARE @intFlag INT;
SET @intFlag = 1;
WHILE ( @intFlag <= @RowCount )
BEGIN
INSERT INTO [dbo].[TableAAA]
( UniqueID ,
company ,
Agent ,
Phone
)
SELECT TOP 1
( SELECT MAX(UniqueID) + 1
FROM [dbo].[TableAAA]
) ,
'New Company' ,
Agent ,
Phone
FROM [dbo].[TableAAA] c
WHERE c.companyid = 'Old Company'
AND c.phone NOT IN ( SELECT Phone
FROM [dbo].[TableAAA]
WHERE company = 'New Company' );
SET @intFlag = @intFlag + 1;
END;
答案 0 :(得分:1)
我会使用MAX(UniqueID)作为SEED,然后以基于集合的方法将其递增
declare @Seed int = (select MAX(UniqueID) FROM [dbo].[TableAAA])
SELECT
ID = row_number() over (order by (select null)) + @Seed
'New Company',
Agent,
Phone
INTO
#Staging
FROM
[dbo].[TableAAA] c
WHERE
c.companyid = 'Old Company'
AND c.phone NOT IN ( SELECT Phone
FROM [dbo].[TableAAA]
WHERE company = 'New Company' )
INSERT INTO [dbo].[TableAAA]
(UniqueID,
company,
Agent,
Phone)
SELECT
ID,
[New Company],
Agent,
Phone
FROM #Staging
DROP TABLE #Staging