我正在尝试构建一个映射表,以将表中新行的ID与它们从中复制的ID相关联.OUTPUT INTO子句似乎是完美的,但它似乎没有按照文档行事.
我的代码:
DECLARE @Missing TABLE (SrcContentID INT PRIMARY KEY ) INSERT INTO @Missing ( SrcContentID ) SELECT cshadow.ContentID FROM Private.Content AS cshadow LEFT JOIN Private.Content AS cglobal ON cshadow.Tag = cglobal.Tag WHERE cglobal.ContentID IS NULL PRINT 'Adding new content headers' DECLARE @Inserted TABLE (SrcContentID INT PRIMARY KEY, TgtContentID INT ) INSERT INTO Private.Content ( Tag, Description, ContentDate, DateActivate, DateDeactivate, SortOrder, CreatedOn, IsDeleted, ContentClassCode, ContentGroupID, OrgUnitID ) OUTPUT cglobal.ContentID, INSERTED.ContentID INTO @Inserted (SrcContentID, TgtContentID) SELECT Tag, Description, ContentDate, DateActivate, DateDeactivate, SortOrder, CreatedOn, IsDeleted, ContentClassCode, ContentGroupID, NULL FROM Private.Content AS cglobal INNER JOIN @Missing AS m ON cglobal.ContentID = m.SrcContentID
结果出现错误信息:
Msg 207, Level 16, State 1, Line 34 Invalid column name 'SrcContentID'.
(第34行是OUTPUT INTO的那一行)
实验表明,只能在OUTPUT INTO中选择实际存在于INSERT目标中的行.但这与网上书籍中的文档相矛盾.关于OUTPUT子句的文章有示例E,它描述了类似的用法:
OUTPUT INTO子句从正在更新的表(WorkOrder)以及Product表中返回值.Product子句用于FROM子句以指定要更新的行.
有没有人使用过这个功能?
(与此同时,我已经使用游标循环重写了我的代码以完成这项工作,但这很丑陋而且我仍然很好奇)
您可以在Sql Server 2008中使用MERGE执行此操作.示例代码如下:
--drop table A create table A (a int primary key identity(1, 1)) insert into A default values insert into A default values delete from A where a>=3 -- insert two values into A and get the new primary keys MERGE a USING (SELECT a FROM A) AS B(a) ON (1 = 0) -- ignore the values, NOT MATCHED will always be true WHEN NOT MATCHED THEN INSERT DEFAULT VALUES -- always insert here for this example OUTPUT $action, inserted.*, deleted.*, B.a; -- show the new primary key and source data
结果是
INSERT, 3, NULL, 1 INSERT, 4, NULL, 2
即,对于每一行,新的主键(3,4)和旧的主键(1,2).创建一个名为#OUTPUT的表并添加"INTO #OUTPUT;" 在OUTPUT子句的末尾将保存记录.
我已经确认问题是你只能使用INSERTED
列.文档似乎表明你可以使用from_table_name
,但我似乎无法使它工作(多部分标识符"m.ContentID"无法绑定.):
TRUNCATE TABLE main SELECT * FROM incoming SELECT * FROM main DECLARE @Missing TABLE (ContentID INT PRIMARY KEY) INSERT INTO @Missing(ContentID) SELECT incoming.ContentID FROM incoming LEFT JOIN main ON main.ContentID = incoming.ContentID WHERE main.ContentID IS NULL SELECT * FROM @Missing DECLARE @Inserted TABLE (ContentID INT PRIMARY KEY, [Content] varchar(50)) INSERT INTO main(ContentID, [Content]) OUTPUT INSERTED.ContentID /* incoming doesn't work, m doesn't work */, INSERTED.[Content] INTO @Inserted (ContentID, [Content]) SELECT incoming.ContentID, incoming.[Content] FROM incoming INNER JOIN @Missing AS m ON m.ContentID = incoming.ContentID SELECT * FROM @Inserted SELECT * FROM incoming SELECT * FROM main
显然,from_table_name
前缀只允许上DELETE
或UPDATE
(或MERGE
2008年) -我不知道为什么:
from_table_name
是一个列前缀,指定包含在或语句的FROM
子句中的表,该表用于指定要更新或删除的行.DELETE
UPDATE
如果在FROM
子句中也指定了要修改的表,则必须使用INSERTED
或DELETED
前缀限定对该表中列的任何引用.
我遇到了与你完全相同的问题,我感到很痛苦...据我所知,没有办法将from_table_name前缀与INSERT语句一起使用.我确信这有一个可行的技术原因,我很想知道它究竟是什么.
好的,发现它,这是一个关于它为什么不起作用的论坛帖子: MSDN论坛