update many rows in Table A with few rows from Table B using merge on stored procedure

102 Views Asked by At

I have data on Table 1 like this: enter image description here

IdT1 , IdT1Group

11 , 30

12 , 30

13 , 30

And On Table 2 Like: enter image description here

IdT2, IdT1, Detail, Synchronise

1 , 11 , A , inserted

2 , 11 , B , inserted

3 , 12 , A , inserted

4 , 12 , C , inserted

I have tblSource like this: enter image description here

IdT2, IdT1, Detail

1 , 11 , A

2 , 11 , B

5 , 11 , C

I got the tblSource from :

Select Top 1 From Tbl2 where IdT1Group = 30

And I hope I can Update Tbl2 to become : enter image description here

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
2

There are 2 best solutions below

1
amit ramkishor gupta On

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

2
amit ramkishor gupta On

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