[英]update many rows in Table A with few rows from Table B using merge on stored procedure
I have data on Table 1 like this: enter image description here 我在表1中有这样的数据: 在此处输入图像描述
IdT1 , IdT1Group IdT1,IdT1Group
11 , 30 11,30
12 , 30 12,30
13 , 30 13,30
And On Table 2 Like: enter image description here 和表2类似: 在此处输入图像描述
IdT2, IdT1, Detail, Synchronise IdT2,IdT1,细节,同步
1 , 11 , A , inserted 1,11,A,插入
2 , 11 , B , inserted 2,11,B,插入
3 , 12 , A , inserted 3,12,A,插入
4 , 12 , C , inserted 4,12,C,插入
I have tblSource like this: enter image description here 我有这样的tblSource: 在这里输入图像描述
IdT2, IdT1, Detail IdT2,IdT1,细节
1 , 11 , A 1,11,A
2 , 11 , B 2,11,B
5 , 11 , C 5,11,C
I got the tblSource from : 我得到了tblSource:
Select Top 1 From Tbl2 where IdT1Group = 30
And I hope I can Update Tbl2 to become : enter image description here 我希望我可以更新Tbl2成为: 在这里输入图像描述
IdT2, IdT1, Detail, Synchronise
1 11 A updated
2 11 B updated
5 11 C inserted
3 12 A updated
4 12 B updated
6 12 C inserted
7 13 A inserted
8 13 B inserted
9 13 C inserted
This is code that I use to get what I expect: 这是我用来获得我期望的代码:
Declare @IdT1Group integer = 30
;WITH tblTbl2
AS (SELECT table2.* FROM table2 INNER JOIN table1 ON table2.IdT1 = table1.IdT1 AND IdT1Group = @IdT1Group)
MERGE INTO tblTbl2 AS tblTarget
USING (SELECT tblT2.*, table1.IdT1 AS T1Id FROM tbl2 AS tblT2 CROSS JOIN table1 where IdT1Group = @IdT1Group)
AS tblSource ON tblTarget.IdT1 = tblSource.T1Id And tblTarget.IdT2 = tblSource.IdT2
WHEN MATCHED THEN
UPDATE
SET Detail = tblSource.Detail, Synchronise = 'updated'
WHEN NOT MATCHED BY SOURCE THEN
DELETE
WHEN NOT MATCHED BY TARGET THEN
INSERT (IdT1, Detail, Synchronise)
VALUES (tblSource.T1Id, tblSource.Detail, 'inserted');
But This is what i got : enter image description here 但这就是我得到的: 在这里输入图像描述
IdT2 IdT1 Detail Synchronise
1 11 A updated
2 11 B updated
5 11 C inserted
6 12 A inserted
7 12 B inserted
8 12 C inserted
9 13 A inserted
10 13 B inserted
11 13 C inserted
This Code for me to do some trial: 本代码对我来说做了一些试用:
drop table if exists table1
create table table1(idt1 int, idt1group int)
insert into table1 values (11,30),(12,30),(13,30)
drop table if exists table2
create table table2 (idt2 int, idt1 int, detail varchar(2), Synchronise varchar(15))
insert into table2 values (1,11,'A', 'Inserted'),(2,11,'B', 'Inserted'),
(3,12,'A', 'Inserted'),
(4,12,'B', 'Inserted')
drop table if exists tbl2
create table tbl2 (idt2 int, idt1 int, detail varchar(2), Synchronise varchar(15))
insert into tbl2 values (1, 11,'A', 'Inserted'),
(2,11,'B', 'Inserted'), (5,11,'C', 'Inserted')
Declare @IdT1Group integer = 30
SELECT table1.IdT1 AS T1Id, tblT2.* FROM tbl2 AS tblT2 CROSS JOIN table1 where IdT1Group = @IdT1Group
;WITH tblTbl2
AS (SELECT table2.* FROM table2 INNER JOIN table1 ON table2.IdT1 = table1.IdT1 AND IdT1Group = @IdT1Group)
MERGE INTO tblTbl2 AS tblTarget
USING (SELECT tblT2.*, table1.IdT1 AS T1Id FROM tbl2 AS tblT2 CROSS JOIN table1 where IdT1Group = @IdT1Group)
AS tblSource ON tblTarget.IdT1 = tblSource.T1Id And tblTarget.IdT2 = tblSource.IdT2
WHEN MATCHED THEN
UPDATE
SET Detail = tblSource.Detail, Synchronise = 'updated'
WHEN NOT MATCHED BY SOURCE THEN
DELETE
WHEN NOT MATCHED BY TARGET THEN
INSERT (IdT1, Detail, Synchronise)
VALUES (tblSource.T1Id, tblSource.Detail, 'inserted');
select * From Table2
You are deleting give data 您正在删除提供数据
3 12 A updated
4 12 B updated
ANd this data does not exists in 这个数据不存在于
Declare @IdT1Group integer = 30
SELECT table1.IdT1 AS T1Id, tblT2.* FROM tbl2 AS tblT2 CROSS JOIN table1 where IdT1Group = @IdT1Group
So it's delete data if you still want to update this then you have to check if data not exists in then update it. 因此,如果您仍想要更新此数据,则删除数据,然后您必须检查数据是否不存在然后更新它。
Declare @IdT1Group integer = 30
SELECT table1.IdT1 AS T1Id, tblT2.* FROM tbl2 AS tblT2 CROSS JOIN table1 where IdT1Group = @IdT1Group
MERGE table2 AS tblTarget
--traget table
USING
--data src
(SELECT table1.IdT1 AS T1Id, tblT2.* FROM tbl2 AS tblT2 CROSS JOIN table1 where IdT1Group = @IdT1Group)
AS tblSource ON tblTarget.IdT1 = tblSource.T1Id And tblTarget.IdT2 = tblSource.IdT2
WHEN MATCHED THEN
UPDATE
SET Detail = tblSource.Detail, Synchronise = 'updated'
--Delete QUery
WHEN NOT MATCHED BY SOURCE THEN
DELETE
--insert query
WHEN NOT MATCHED BY TARGET THEN
INSERT ([idt2],IdT1, Detail, Synchronise)
VALUES (tblSource.idt2,tblSource.T1Id, tblSource.Detail, 'inserted');
select * From Table2
let me know if you still have question 如果你还有疑问,请告诉我
try this 尝试这个
--data query
drop table table1
create table table1(idt1 int, idt1group int)
insert into table1 values (11,30),(12,30),(13,30)
drop table table2
create table table2 (idt2 int, idt1 int, detail varchar(2), Synchronise varchar(15))
insert into table2 values (1,11,'A', 'Inserted'),(2,11,'B', 'Inserted'),
(3,12,'A', 'Inserted'),
(4,12,'B', 'Inserted')
drop table tbl2
create table tbl2 (idt2 int, idt1 int, detail varchar(2), Synchronise varchar(15))
insert into tbl2 values (1, 11,'A', 'Inserted'),
(2,11,'B', 'Inserted'), (5,11,'C', 'Inserted')
--main query
Declare @IdT1Group integer = 30
SELECT table1.IdT1 AS T1Id, tblT2.* FROM tbl2 AS tblT2 CROSS JOIN table1 where IdT1Group = @IdT1Group
MERGE table2 AS tblTarget
--traget table
USING
--data src
(SELECT table1.IdT1 AS T1Id, tblT2.* FROM tbl2 AS tblT2 CROSS JOIN table1 where IdT1Group = @IdT1Group)
AS tblSource ON tblTarget.IdT1 = tblSource.T1Id And tblTarget.IdT2 = tblSource.IdT2
WHEN NOT MATCHED BY SOURCE THEN
UPDATE
SET Synchronise = 'updated';
MERGE table2 AS tblTarget
--traget table
USING
--data src
(SELECT table1.IdT1 AS T1Id, tblT2.* FROM tbl2 AS tblT2 CROSS JOIN table1 where IdT1Group = @IdT1Group)
AS tblSource ON tblTarget.IdT1 = tblSource.T1Id And tblTarget.IdT2 = tblSource.IdT2
WHEN MATCHED THEN
UPDATE
SET Detail = tblSource.Detail, Synchronise = 'updated'
--insert query
WHEN NOT MATCHED BY TARGET THEN
INSERT ([idt2],IdT1, Detail, Synchronise)
VALUES (tblSource.idt2,tblSource.T1Id, tblSource.Detail, 'inserted');
select * From Table2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.