-1

到目前为止我有这个代码:

DECLARE @AddressIDS TABLE
(
    AddressID int
)

INSERT INTO Address(Street1, City, StateCode, ZipCode)
OUTPUT inserted.AddressID INTO @AddressIDS
SELECT Street1, City, StateCode, ZipCode
    FROM Contact

现在我需要将 ID 放在表的@AddressIDS后面。AddressIDContact

这更好地描述了我在 TSQL 中尝试做的事情:

在此处输入图像描述

4

1 回答 1

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;
于 2012-07-05T18:58:09.167 回答