Reputation: 69
I have two tables as below and trying to get result as specified using merge. Is it possible?
Table 1:
Table1ID Q1A Q2A Q3A Active
----------------------------------------
1 2 Test 1 1
2 3 Test2 1 1
3 4 Test3 1 1
4 5 Test4 1 1
5 5 Test5 1 0
I found an other condition and having issue with it, there also exists data for row 5 which is inactive in this case instead of updating that row any way, i need to add new row with same details but a active row. Any suggestions on how to update the script. Identity column exists for table1 so data wont be duplicated.
Compare data from table2 and update data in table 1 while looking through existing records.
Table2:
Table1ID E1 E2 E3
----------------------------------------
1 2 TestData1 1
2 3 TestData2 1
3 4 TestData3 1
5 5 TestData5 1
6 7 TestData6 0
Result:
Table1ID Q1A Q2A Q3A Active
-----------------------------------------
1 2 Test 1 1 -- Rows with Id 1,2,3 already exists in table 1 dont do anything
2 3 Test2 1 1
3 4 Test3 1 1
4 5 Test4 1 0 -- Rows with Id 4 exists in table 1 but not in table 2 update it to inactive
5 5 Test5 1 0
5 5 Test5 1 1 -- Rows with Id 5,6 does not exist in table 1 so insert it
6 7 Test6 0 1
Upvotes: 1
Views: 434
Reputation: 36
I was able to match the result exactly using the below script. Hope this helps! Let me know if you have any questions.
/*
Script to create the tables and merge the data
*/
-- drop table #Table1;
create table #Table1 (
Table1ID INT,
Q1A INT,
Q2A varchar(10),
Q3A INT,
Active BIT
);
INSERT INTO #Table1 VALUES (1, 2, 'Test', 1, 1);
INSERT INTO #Table1 VALUES (2, 3, 'Test2', 1, 1);
INSERT INTO #Table1 VALUES (3, 4, 'Test3', 1, 1);
INSERT INTO #Table1 VALUES (4, 5, 'Test4', 1, 1);
-- drop table #Table2;
create table #Table2 (
Table1ID INT,
E1 INT,
E2 varchar(10),
E3 INT
);
INSERT INTO #Table2 VALUES (1, 2, 'TestData1', 1);
INSERT INTO #Table2 VALUES (2, 3, 'TestData2', 1);
INSERT INTO #Table2 VALUES (3, 4, 'TestData3', 1);
INSERT INTO #Table2 VALUES (5, 5, 'TestData5', 1);
INSERT INTO #Table2 VALUES (6, 7, 'TestData6', 0);
;MERGE INTO #Table1 AS TGT
USING #Table2 AS SRC
ON TGT.Table1ID = SRC.Table1ID
WHEN NOT MATCHED BY SOURCE THEN UPDATE
SET TGT.Active = CAST(0 AS BIT)
WHEN NOT MATCHED THEN INSERT (Table1ID, Q1A, Q2A, Q3A, Active)
VALUES (SRC.Table1ID, SRC.E1, REPLACE(SRC.E2, 'Data', ''), SRC.E3, CAST(1 AS BIT));
select *
from #Table1;
You can also use variables in place of a table as follows:
DECLARE @Table1ID INT = 1,
@E1 INT = 2,
@E2 varchar(10) = 'TestData1',
@E3 INT = 1;
;MERGE INTO #Table1 AS TGT
USING (VALUES(@Table1ID, @E1, @E2, @E3))
AS SRC (Table1ID, E1, E2, E3)
ON TGT.Table1ID = SRC.Table1ID
WHEN NOT MATCHED BY SOURCE THEN UPDATE
SET TGT.Active = CAST(0 AS BIT)
WHEN NOT MATCHED THEN INSERT (Table1ID, Q1A, Q2A, Q3A, Active)
VALUES (SRC.Table1ID, SRC.E1, REPLACE(SRC.E2, 'Data', ''), SRC.E3, CAST(1 AS BIT));
select *
from #Table1;
Upvotes: 2
Reputation: 2032
Please try this -
merge @table1 t1
using @table2 t2 on t2.table1_id = t1.id
when not matched by source
then update set t1.active = 0
when not matched by target then
insert (id, q1a, q2a, q3a, active)
values (t2.table1_id, t2.e1, t2.e2, t2.e3, 1);
Data script-
declare @table1 table (id int, q1a int, q2a varchar(10), q3a int, active bit)
declare @table2 table (table1_id int, e1 int, e2 varchar(10), e3 int)
insert into @table1
select 1,2,'test',1,1 union
select 2,3,'test2',1,1 union
select 3,4,'test3',1,1 union
select 4,5,'test4',1,1
insert into @table2
select 1,2,'testdata1',1 union
select 2,3,'testdata2',1 union
select 3,4,'testdata3',1 union
select 5,5,'testdata5',1 union
select 6,7,'testdata6',0
select * from @table1
select * from @table2
Upvotes: 2