Microsoft 的 OUTPUT 子句文档说您可以在 OUTPUT 子句的列名中使用from_table_name 。
有两个例子:
是否也可以在INSERT 语句中使用它?
INSERT INTO T ( [Name] )
OUTPUT S.Code, inserted.Id INTO @TMP -- The multi-part identifier "S.Code" could not be bound.
SELECT [Name] FROM S;
使用表变量的失败示例
-- A table to insert into.
DECLARE @Item TABLE (
[Id] [int] IDENTITY(1,1),
[Name] varchar(100)
);
-- A table variable to store inserted Ids and related Codes
DECLARE @T TABLE (
Code varchar(10),
ItemId int
);
-- Insert some new items
WITH S ([Name], Code) AS (
SELECT 'First', 'foo'
UNION ALL SELECT 'Second', 'bar'
-- Etc.
)
INSERT INTO @Item ( [Name] )
OUTPUT S.Code, inserted.Id INTO @T -- The multi-part identifier "S.Code" could not be bound.
SELECT [Name] FROM S;