如何将插入的输出值分配给sql server中的变量?

时间:2012-09-24 13:53:03

标签: sql sql-server sql-server-2008

  

可能重复:
  SQL Server Output Clause into a scalar variable

DECLARE @id int
INSERT INTO MyTable(name)
OUTPUT @id = Inserted.id
VALUES('XYZ')

我正如上所述。怎么可能?

2 个答案:

答案 0 :(得分:73)

使用表变量获取id

DECLARE @id int
DECLARE @table table (id int)
INSERT INTO MyTable(name)
OUTPUT inserted.id into @table
VALUES('XYZ')

SELECT @id = id from @table

答案 1 :(得分:0)