Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何从';从';查询的输出部分中的表_Sql_Sql Server_Sql Server 2012 - Fatal编程技术网

Sql 如何从';从';查询的输出部分中的表

Sql 如何从';从';查询的输出部分中的表,sql,sql-server,sql-server-2012,Sql,Sql Server,Sql Server 2012,这是我的疑问: INSERT INTO Translated OUTPUT @ChangeID,Inserted.ResourceID,Project,Inserted.TranslatedValue into backup Select NEWID(),ResourceID,TranslatedValue,getdate() from Resources where Project ='blahblah' 现在我无法在输出部分获取Project的值,因为它来自资源表,但未插入。有没有简单的

这是我的疑问:

INSERT INTO Translated
OUTPUT @ChangeID,Inserted.ResourceID,Project,Inserted.TranslatedValue into backup
Select NEWID(),ResourceID,TranslatedValue,getdate()
from Resources 
where Project ='blahblah'

现在我无法在输出部分获取Project的值,因为它来自资源表,但未插入。有没有简单的方法可以在不修改查询结构的情况下获取值?

输出子句只能引用插入的行。您可以在查询后更新备份表

update  b
set     project = r.projcet
from    backup b
join    resources r
on      b.ResourceID = r.ResourceID
where   b.project is null

您必须执行第二个查询,将输出表连接到资源,以获取插入的每一行的项目值

可能是以下情况:

Select Project
From   Backup B
Inner Join Resources R on B.ResourceID = R.ResourceID
where B.Project is null

您使用的SQL Server版本是否可能重复?@GarethD,SQL Server 2012我没有正式将此标记为重复,因为我不确定您所说的“不修改查询结构”的确切含义-将查询更改为使用
MERGE
而不是
INSERT
是否比您希望的修改更大?还是它解决了你的问题?@GarethD,因为实际的查询是巨大而混乱的,我不想使用合并而不是插入,下面的答案解决了我的问题,如果没有新的答案,我将接受其中一个。