输出到 SQL Server 2005 中的临时表

问题描述:

我正在尝试使用 OUTPUT 子句 在存储过程中INSERT之后的标识列的值输出到临时表>.

I am trying to use the OUTPUT clause inside a stored procedure to output to a temporary table the values of an indentity column after an INSERT.

CREATE TABLE #Test
(
    ID INT
)

INSERT INTO [TableB] OUTPUT INSERTED.ID #Test SELECT * FROM [TableA]

但是,当我执行此过程时,SQL Server 向我显示了一个名为 Test 的表(正确)中的结果,但是如果我将 SELECT * FROM #Test 写为下一个存储过程中的语句它没有显示任何内容.我怎样才能有效地做到这一点?

However, when I execute this procedure SQL Server shows me the results in a table (correctly) called Test but if I write SELECT * FROM #Test as the next statement in the stored procedure it shows me nothing. How can I efectively accomplish this?

我认为您缺少一个 INTO - 试试这个:

I think you're missing an INTO - try this:

CREATE TABLE #Test(ID INT)

INSERT INTO [TableB] 
    OUTPUT INSERTED.ID INTO #Test 
    SELECT * FROM [TableA]

在列到OUTPUT的列表之后,在表名前添加一个INTO

After the list of columns to OUTPUT, add an INTO before the table name