当前位置:Gxlcms > mysql > SqlServer在事务中获得自增ID

SqlServer在事务中获得自增ID

时间:2021-07-01 10:21:17 帮助过:38人阅读

感谢lee576的分享精神,代码如下: USE tempdbgoCREATE TABLE table1( id INT, employee VARCHAR(32))goINSERT INTO table1 VALUES(1, one)INSERT INTO table1 VALUES(2, two)INSERT INTO table1 VALUES(3, three)INSERT INTO table1 VALUES(4, four)GOCREATE

感谢 lee576 的分享精神,代码如下:

USE tempdb
go

CREATE TABLE table1
(
    id INT,
    employee VARCHAR(32)
)
go

INSERT INTO table1 VALUES(1, 'one')
INSERT INTO table1 VALUES(2, 'two')
INSERT INTO table1 VALUES(3, 'three')
INSERT INTO table1 VALUES(4, 'four')
GO

CREATE TABLE table2
(
    id INT IDENTITY(1,1),
    employee VARCHAR(32)
)
GO

--=================================
--  用OUTPUT把ID插入@MyTableVar
--=================================
DECLARE @MyTableVar TABLE
(
    id INT
)

INSERT INTO table2 (employee) 
OUTPUT INSERTED.id INTO @MyTableVar
SELECT employee FROM table1

--=================================
--  查询@MyTableVar
--=================================
SELECT * FROM @MyTableVar

--DROP TABLE table1,table2

人气教程排行