简体   繁体   English

比较两个表并使用 SQL Server 过程在第三个表中插入具有添加或删除状态的所有记录

[英]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.

相关问题 比较两个表并在 SQL Server 过程中将记录与状态合并 - Compare two tables and combine records with status in SQL Server procedure SQL比较2 #temp表并将差异插入第3 #temp表 - SQL Compare 2 #temp tables and insert the differences into a 3rd #temp table SQL 服务器存储过程统计两个表中的匹配记录并将数字插入另一个表 - SQL Server stored procedure to count matching records in two tables and insert number into another table SQL - 比较2个表,然后将唯一项添加到第3个表 - SQL - Compare 2 tables then add unique items to 3rd table 如何从2个表中进行区分联合并将数据插入第3个表-SQL Server 2017 - How do I do distinct union from 2 tables and insert data to a 3rd table - SQL Server 2017 SQL Server从2个不同的表中获取值,没有匹配,并插入到第3个表中 - SQL Server taking values from 2 different tables with no matching and insert into 3rd table 如何使用SQL检索表中所有记录的所有3级子节点? - How can I retrieve all 3rd level child nodes for all records in my table using SQL? SQL Server基于2个表中的列在第3个表中创建行 - SQL Server Create rows in 3rd table based on columns in 2 tables Postgres SQL 使用第三个表中的 id 从两个表中收集数据 - Postgres SQL gathering data from two tables using an id from a 3rd table 将 2 个表之间的差异插入到第 3 个表中 - Insert differences between 2 tables into 3rd table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM