仅当Employee在Table1中而不在Table2中,或者Employee Information从Table1变为Table2并添加了一个已更改对象时,我试图编写一个脚本来将数据从Table1插入Table2。
样本数据:
表1:
GetPasswordHashAsync
表2:
EmployeeNumber - Name - HireDate - TermDate
-------------------------------------------
1234 - Ted - 8-12-1980 - NULL
9632 - Josh - 1-1-2019 - 3-29-2019
5678 - Jeff - 3-29-2019 - NULL
我已经尝试了好几次插入和更新查询,但是没有运气。
此查询用于插入不存在的记录。
EmployeeNumber - Name - HireDate - TermDate - ChangeDate
---------------------------------------------------------
1234 - Ted - 8-12-1980 -NULL - 8-12-1980
9632 - Josh - 1-1-2019 - NULL - 1-1-2019
查询运行后,Table2应该如下所示:
表2:
INSERT INTO Table2 ([EmployeeNumber], [Name], [HireDate], [Termdate], [ChangeDate])
SELECT
*, GETDATE() AS 'ChangeDate'
FROM
Table1
WHERE
NOT EXISTS (SELECT [EmployeeNumber]
FROM Table2
WHERE Table1.[EmployeeNumber] = Table2.[EmployeeNumber])