我正在尝试将某些行复制到同一个表中。只想更新一个主键列。我怎么能通过增量变量来做到这一点。
USE [Target Database]
GO
BEGIN
DECLARE @x INT=0
SET @x=@x+1
INSERT INTO [dbo].[Target_Table] ([Primary_Key],[Col1],[Col2])
SELECT CONCAT('50487' ,FORMAT( @x ,'000') ) as [Primary_Key],[Col1],[Col2]
FROM [dbo].[Target_Table]
WHERE [dbo].[Target_Table].col1=54706
END
示例现有数据:
Primary_Key Col1 Col2 50409001 54706 Test1 . . . 50409050 54706 Test2
我想这样做:
Primary_Key Col1 Col2 50487001 54706 Test1 . . . 50487050 54706 Test2
答案 0 :(得分:0)
也许是这样的
drop table tmp;
go
create table tmp(id int primary key, col1 int, col2 int)
go
truncate table tmp
insert into tmp values (1,10,100),(2,20,2),(3,10,200);
insert into tmp
select 4000 + ROW_NUMBER() OVER (ORDER BY ID) - 1 rn,col1,col2
from tmp
where col1 = 10
select * from tmp
id col1 col2
----------- ----------- -----------
1 10 100
2 20 2
3 10 200
4000 10 100
4001 10 200
(5 row(s) affected)
答案 1 :(得分:0)
以下应该可以帮助您:
appsettings.json