【发布时间】:2020-03-25 12:26:15
【问题描述】:
Microsoft's OUTPUT Clause documentation 表示您可以在 OUTPUT 子句的列名中使用 from_table_name。
有两个例子:
- Using OUTPUT INTO with from_table_name in an UPDATE statement
- Using OUTPUT INTO with from_table_name in a DELETE statement
是否也可以在 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;
【问题讨论】:
-
没有插入的源列只能被MERGE引用/输出
-
@SMor 是的,确实如此!这是我找不到的重复答案。
标签: sql sql-server tsql output-clause