[英]Compare two tables and insert all records with added or removed status in 3rd table using SQL Server procedure
I have table A and table B .我有表 A 和表 B 。 I have to compare this tables records and insert data to table C using SQL Server procedure in below format我必须使用以下格式的 SQL Server 过程比较这些表记录并将数据插入表 C
table A表A
name
A
B
C
D
E
F
G
table B表B
name
A
B
Q
C
D
F
G
table c should be like below.表 c 应如下所示。 it has an extra field 'status' to mention record is added or removed.它有一个额外的字段“状态”来提及添加或删除记录。
name status
A
B
Q newly added
C
D
E removed
F
G
I know we can compare 2 tables and find added or removed records using EXCEPT and UNION operations.我知道我们可以比较 2 个表并使用 EXCEPT 和 UNION 操作查找添加或删除的记录。 But in this case, I have to integrate that records with unchanged records and should place that added or removed records in correct position.但在这种情况下,我必须将这些记录与未更改的记录整合在一起,并且应该将添加或删除的记录放置在正确的位置。
You can do this with a full join
and conditional logic:您可以使用full join
和条件逻辑来执行此操作:
select
coalesce(a.name, b.name) name,
case
when a.name is null then 'newly added'
when b.name is null then 'removed'
end status
from tablea a
full join tableb b on b.name = a.name
order by name
Demo on DB Fiddle : DB Fiddle 上的演示:
name | status :--- | :---------- A | null B | null C | null D | null E | removed F | null G | null Q | newly added
Depending on which order do you want to accomplish at the end you can use this:根据您希望最终完成的顺序,您可以使用以下命令:
select name, max(status), descr from(
select
coalesce(a.col, b.col) name,
coalesce(a.descr, b.descr) descr,
case
when a.col is null then 'newly added'
when b.col is null then 'removed'
end status
, ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) rn
from a a
left join b b on b.col = a.col
union
select
coalesce(a.col, b.col) name,
coalesce(a.descr, b.descr) descr,
case
when a.col is null then 'newly added'
when b.col is null then 'removed'
end status
, ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) rn
from b b
left join a a on b.col = a.col) A
group by name, descr
order by max(rn);
And then if you want to order by how it is in table a then in first select select from b left join a
and in your second select from a left join b
and if you want to oder by how it is in table b then in first select select from a left join b
and in your second select from b left join a
.然后,如果您想按表 a 中的方式排序,则首先选择 select from b left join a
并在第二个 select from a left join b
排序,如果您想按表from b left join a
方式排序,则在 first select select from a left join b
并在第二个 select from b left join a
。
Here is a demo with the last requested samle data. 这是一个带有最后请求的示例数据的演示。
You could try using a some union and left join您可以尝试使用 some union 和 left join
select A.name, case when is null t1.name then 'newly addedd' end
from A
left JOIN (
select A.name from A
union B.name from B
) t1
union
select B.name, case when is null t1.name then 'delete' end
from B
left JOIN (
select A.name from A
union B.name from B
) t1
please try with below query ( SQL FIDDLE ):请尝试使用以下查询( SQL FIDDLE ):
CREATE PROCEDURE update_records
AS
BEGIN
INSERT INTO C(name, status)
SELECT AB.name, AB.status FROM(
SELECT (case when A.name is null then B.name else A.name end) as name,
(CASE
WHEN A.name is null THEN 'newly added'
WHEN B.name is null THEN 'removed'
END) AS status
FROM A
FULL JOIN B on B.name = A.name
) as AB
ORDER by AB.name
END
You should use FULL OUTER JOIN.您应该使用 FULL OUTER JOIN。
DECLARE @table1 TABLE( [name] char(1) ) DECLARE @table2 TABLE( [name] char(1) ) INSERT INTO @table1 VALUES ('A'),('B'),('C'),('D'),('E'),('F'),('G') INSERT INTO @table2 VALUES ('A'),('B'),('Q'),('C'),('D'),('F'),('G') SELECT IIF(T1.name IS NULL,T2.name,T1.name) as 'Name', CASE WHEN T1.name IS NULL THEN 'newly added' WHEN T2.name IS NULL THEN 'removed' ELSE '' END as 'Status' FROM @table1 T1 FULL OUTER JOIN @table2 T2 ON T1.name = T2.name
There ise one more possible method:还有一种可能的方法:
DECLARE @table1 TABLE( [name] char(1) ) DECLARE @table2 TABLE( [name] char(1) ) INSERT INTO @table1 VALUES ('A'),('B'),('C'),('D'),('E'),('F'),('G') INSERT INTO @table2 VALUES ('A'),('B'),('Q'),('C'),('D'),('F'),('G') SELECT T1.name as 'Full_List', IIF(T2.name IS NOT NULL,'','removed') as 'Status' FROM @table1 T1 LEFT OUTER JOIN @table2 T2 ON T1.name = T2.name UNION ALL SELECT T2.name, IIF(T1.name IS NULL,'Added','') FROM @table2 T2 LEFT OUTER JOIN @table1 T1 ON T1.name = T2.name WHERE T1.name IS NULL
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.