本文介紹了如何使用 TSQL 腳本將一個表拆分為另一個表的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
到目前為止我有這個代碼:
I have this code so far:
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 放回到 Contact
表的 AddressID
中.
Now I need to put the IDs in the @AddressIDS
back into the AddressID
of the Contact
table.
這更好地描述了我在 TSQL 中嘗試做的事情:
This better describes what I'm trying to do in TSQL:
推薦答案
假設 Street1 + City + StateCode + ZipCode 是唯一的,或者多個聯系人可以共享同一個 AddressID,那么您就不需要推測 AddressID 值在一個中間步驟.您只需執行插入地址,然后:
Assuming Street1 + City + StateCode + ZipCode is unique, or that multiple contacts can share the same AddressID, then you don't need to suss out the AddressID values in an intermediate step. You can just perform your insert into Address and then:
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;
這篇關于如何使用 TSQL 腳本將一個表拆分為另一個表的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!