[英]SQL - How to use the output from an insert to update a table
Detailed Question 详细问题
The best way I can explain my question is to explain my desired outcome. 我能解释我问题的最好方法是解释我想要的结果。 I'm trying to take a certain set of offices, insert its data into the dbo.DeliveryLocation table, then take the output inserted.DeliveryLocationId and update the corresponding office's DeliveryLocationId field with that id. 我正在尝试使用某组办公室,将其数据插入dbo.DeliveryLocation表,然后输出inserted.DeliveryLocationId并使用该id更新相应office的DeliveryLocationId字段。
Desired Outcome Example 期望的结果例子
Office Data Before 办公室数据之前
OfficeId | DeliveryLocationId
-----------------------------
1 | null
2 | null
3 | null
Run the SQL statement 运行SQL语句
Office Data After 办公室数据之后
OfficeId | DeliveryLocationId
-----------------------------
1 | 5
2 | 6
3 | 7
Delivery Location with the DeliveryLocationId of 5 was created with the data of the Office with OfficeId of 1 使用OfficeId为1的Office数据创建DeliveryLocationId为5的交付位置
Delivery Location with the DeliveryLocationId of 6 was created with the data of the Office with OfficeId of 2 使用OfficeId为2的Office数据创建DeliveryLocationId为6的交付位置
Delivery Location with the DeliveryLocationId of 7 was created with the data of the Office with OfficeId of 3 使用OfficeId为3的Office数据创建DeliveryLocationId为7的交付位置
The problem 问题
Per my current SQL script below, you can see that I have the first part (inserting the Office data into the Delivery Location table) complete. 根据我下面的当前SQL脚本,您可以看到我的第一部分(将Office数据插入交付位置表)完成。 The second part (updating the Office with the corresponding DeliveryLocationId of the created Delivery Location) is not complete, and I am unsure how to go about doing that. 第二部分(使用创建的交付位置的相应DeliveryLocationId更新Office)未完成,我不确定如何执行此操作。
My initial thoughts/ solutions 我最初的想法/解决方案
If there would be a way to store the correlated OfficeId and DeliveryLocationId, perhaps we could loop through them and update the offices in a second SQL statement rather than try to create one SQL statement that does everything. 如果有一种方法来存储相关的OfficeId和DeliveryLocationId,也许我们可以循环遍历它们并在第二个SQL语句中更新办公室,而不是尝试创建一个完成所有操作的SQL语句。
dbo.DeliveryLocation dbo.DeliveryLocation
[DeliveryLocationId] [int] IDENTITY(1,1) NOT NULL,
[LocationName] [nvarchar](max) NULL,
[ShortName] [nvarchar](max) NULL,
[ValidatedAddressId] [int] NOT NULL,
[DropoffInstruction] [nvarchar](max) NULL,
[PickupInstruction] [nvarchar](max) NULL,
[TaxRate] [decimal](18, 2) NOT NULL,
[Active] [bit] NOT NULL,
[DisableOffices] [bit] NOT NULL
dbo.Office dbo.Office
[OfficeId] [int] IDENTITY(1,1) NOT NULL,
[OfficeName] [nvarchar](max) NULL,
[ValidatedAddressId] [int] NOT NULL,
[ReferralSource] [nvarchar](max) NOT NULL,
[NumberOfEmployees] [int] NOT NULL,
[DeliveryLocationId] [int] NULL
Current SQL 当前的SQL
insert into
dbo.DeliveryLocation
(LocationName, ShortName, ValidatedAddressId, Active, DisableOffices)
output
inserted.DeliveryLocationId
select
OfficeName, OfficeName, ValidatedAddressId, 0, 0
from
dbo.Office as o
where
OfficeId in
(
select distinct
OfficeId
from
dbo.[User] as u
where
u.DeliveryLocationId is null
and
u.OfficeId is not null
)
I'm not sure about doing this in an INSERT
statement, but if you use a MERGE
statement using Office (or a query based on Office) as the source, you'll be able to refer to source.OfficeId as well as inserted.DeliveryLocationId in the OUTPUT
clause. 我不确定在INSERT
语句中执行此操作,但如果您使用使用Office(或基于Office的查询)作为源的MERGE
语句,您将能够引用source.OfficeId以及插入。 OUTPUT
子句中的DeliveryLocationId。 You can simply skip the update and delete usage of the MERGE
, and only use the ON NOT MATCHED
clause. 您可以简单地跳过更新并删除MERGE
用法,并且只使用ON NOT MATCHED
子句。
When I'm doing things like this I put the output into a temp table, then carry out any further updates or inserts I need to do from there. 当我做这样的事情时,我将输出放入临时表,然后执行我需要做的任何进一步的更新或插入。
In case you've not used the MERGE
statement before (or even for anyone who just hasn't used all of their capabilities), this is a really fantastic resource on how to use them, and how to use them well: http://www.made2mentor.com/2012/07/got-the-urge-to-merge/ 如果您以前没有使用MERGE
语句(或者甚至没有使用过他们所有功能的人),这是一个非常棒的资源,如何使用它们,以及如何使用它们: http:/ /www.made2mentor.com/2012/07/got-the-urge-to-merge/
You could do an update join after you insert into delivery location 插入到交货地点后,您可以执行更新连接
update dbo.Office
set o.DeliveryLocationID = dl.DeliveryocationID
from Office o
JOIN DeliveryLocation dl on dl.LocationName = o.OfficeName
Assuming that the Office Names are unique. 假设Office名称是唯一的。 If they are not you may want to add OfficeID to the DeliveryLocations table, at least temporarily, and join on that. 如果不是,您可能希望至少暂时将OfficeID添加到DeliveryLocations表,并加入其中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.