您可以在临时表中选择您的数据,然后在插入行时在其上运行游标,您将拥有 PK
DECLARE @tempTable TABLE(
a VARCHAR(MAX),
b VARCHAR(MAX),
c VARCHAR(MAX)
)
INSERT INTO @tempTable(a,b,c)
SELECT a,b,c FROM my_table WHERE x=y
DECALRE @a DATATYPE;
Declare @b DATATYPE;
Declare @c DATATYPE;
DECLARE cursor CURSOR FOR
SELECT * FROM @tempTable
OPEN cursor
FETCH NEXT FROM PropertyCursor INTO @a, @b,@c
WHILE (@@FETCH_STATUS = 0)
BEGIN
INSERT INTO my_table(a,b,c)
VALUES(@a,@b,@c)
DECALRE @PrimaryKey INT = SCOPE_IDENTITY();//THis line will give u the PK so u can work with it and then below move to next row
FETCH NEXT FROM cursor INTO @a, @b,@c
END
CLOSE cursor
DEALLOCATE cursor