到目前为止我有这个代码:
DECLARE @AddressIDS TABLE
(
AddressID int
)
INSERT INTO Address(Street1, City, StateCode, ZipCode)
OUTPUT inserted.AddressID INTO @AddressIDS
SELECT Street1, City, StateCode, ZipCode
FROM Contact
现在我需要将@AddressIDS
中的ID重新放回AddressID
表的Contact
。
这更好地描述了我在TSQL中要做的事情:
答案 0 :(得分:0)
假设Street1 + City + StateCode + ZipCode是唯一的,或者多个联系人可以共享相同的AddressID,那么您不需要在中间步骤中输出AddressID值。您只需执行插入地址,然后执行:
UPDATE c
SET AddressID = a.AddressID
FROM dbo.Contact AS c
INNER JOIN dbo.Address AS a
ON c.Street1 = a.Street1
AND c.City = a.City
AND c.StateCode = a.StateCode
AND c.ZipCode = a.ZipCode;