How can I split off one table into another table using TSQL script(如何使用 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 脚本将一个表拆分为另一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 TSQL 脚本将一个表拆分为另一个表
基础教程推荐
- 从字符串 TSQL 中获取数字 2021-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- 带更新的 sqlite CTE 2022-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
