我有2张这样的桌子:
CREATE TABLE t1
(
[SupplyTitle] [NVARCHAR](50) NOT NULL,
[EmployeeCode] INT NOT NULL,
[registered] DATETIME,
[modified] DATETIME
)
CREATE TABLE t2
(
[SupplyTitle] [NVARCHAR](50) NOT NULL,
[EmployeeCode] INT NOT NULL,
[AnnouncementID] INT NOT NULL
[registered] DATETIME,
[modified] DATETIME
)
t1中的记录如下:
SupplyTitle EmployeeCode registered modified
-----------------------------------------------------------------------------
aaa 9001 2018-11-15 15:41:36.613 2018-11-15 15:41:36.613
bbb 9002 2018-11-15 15:42:36.613 2018-11-15 15:42:36.613
ccc 9003 2018-11-15 15:43:36.613 2018-11-15 15:43:36.613
t2中的AnnouncementID
是通过以下过程R_GetManageID
生成的:
DECLARE @ManageID TABLE(ManageID INT)
DECLARE @AnnouncementID int
INSERT INTO @ManageID
EXEC R_GetManageID
SET @AnnouncementID = (SELECT TOP 1 * FROM @ManageID)
SELECT @AnnouncementID
我想将数据从t1插入到t2。
插入规则为:
如果t2中没有t1的记录,则将其插入到t2中,相关条件为t1.SupplyTitle = t2.SupplyTitle and t1.EmployeeCode = t2.EmployeeCode
。
如果t2中存在t1记录,则只需更新t2.modified = getdate()
。
这意味着t2的AnnouncementID
仅在不满足相关条件时才生成@AnnouncementID
。
例如,一开始,T2为空。
t1中的记录如下:
SupplyTitle EmployeeCode registered modified
aaa 9001 2018-11-15 15:41:36.613 2018-11-15 15:41:36.613
bbb 9002 2018-11-15 15:42:36.613 2018-11-15 15:42:36.613
ccc 9003 2018-11-15 15:43:36.613 2018-11-15 15:43:36.613
如果@AnnouncementID
的初始值为1001,则每次将此值加1。
然后在t2中生成的记录如下:
SupplyTitle EmployeeCode AnnouncementID registered modified
aaa 9001 1001 2018-11-19 12:00:00.000 2018-11-19 12:00:00.000
bbb 9002 1002 2018-11-19 12:00:00.000 2018-11-19 12:00:00.000
ccc 9003 1003 2018-11-19 12:00:00.000 2018-11-19 12:00:00.000
然后将t1中的记录更改为此:
SupplyTitle EmployeeCode registered modified
aaa 9001 2018-11-15 15:41:36.613 2018-11-15 15:41:36.613
bbb 9002 2018-11-15 15:42:36.613 2018-11-15 15:42:36.613
www 9008 2018-11-15 15:43:36.613 2018-11-15 15:43:36.613
所以t2中的记录应该像这样添加一行:
SupplyTitle EmployeeCode AnnouncementID registered modified
aaa 9001 1001 2018-11-19 12:00:00.000 2018-11-19 12:00:00.000
bbb 9002 1002 2018-11-19 12:00:00.000 2018-11-19 12:30:00.000
ccc 9003 1003 2018-11-19 12:00:00.000 2018-11-19 12:30:00.000
www 9008 1004 2018-11-19 12:30:00.000 2018-11-19 12:30:00.000
前2条记录的modified
已更改。
那么我怎么能像这样插入(更新)t2?
答案 0 :(得分:0)
使用MERGE
可帮助您同步两个表,如下所示:
merge t2 as t
using t1 as s
on
(
t.SupplyTitle = s.SupplyTitle
and
t.EmployeeCode = s.EmployeeCode
)
when matched then
update
set
t.registered = s.registered, --any other data
t.modified = s.modified,
t.AnnouncementID = 0--'your_data'
when not matched by target then
insert
(
SupplyTitle,
EmployeeCode,
AnnouncementID,
registered,
modified
)
values
(
s.SupplyTitle,
s.EmployeeCode,
0,--'your_data',
s.registered,
s.modified
)
output $action,
deleted.SupplyTitle as target_SupplyTitle,
deleted.EmployeeCode as target_EmployeeCode,
deleted.registered as target_registered,
deleted.modified as target_modified,
inserted.SupplyTitle as source_SupplyTitle,
inserted.EmployeeCode as source_EmployeeCode,
inserted.registered as source_registered,
inserted.modified as source_modified;
看看Reference Link以了解其工作原理。