比较两个不同表的相同列的不匹配数据

时间:2017-11-21 00:26:50

标签: sql-server database

我有以下查询来检查用户名是否与表之间完全相同

SELECT 
  --Username
  a.username as 'TABLE_1_USERNAME', 
  b.username as 'TABLE_2_USERNAME',
  CASE
      WHEN a.username IS NULL AND b.username IS NULL THEN 'True'  
      WHEN a.username = b.username THEN 'True'  
      ELSE 'False' 
  END AS 'Is Equal?'
  FROM User a 
JOIN User_Group b ON a.id = b.id

这很有用,告诉我用户名是否因任何原因而有所不同,但我想要做的是递归地比较User表和User_Group表之间的每一列(无需编写)每一个) - 两个表的列名相同。这在SQL Server中是否可行?

为了简洁而省略套管检查和修剪

2 个答案:

答案 0 :(得分:0)

试试这个。 Except显示两个查询中不完全匹配的任何行

SELECT * FROM User_Group
EXCEPT
SELECT * FROM User 

答案 1 :(得分:0)

这是一个动态的sql方法,用于按字面意思执行你所要求的:

DECLARE @cols TABLE (id int identity(1,1), colname varchar(25))
INSERT INTO @cols (colname) select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'User' AND COLUMN_NAME <> 'id'

declare @sql nvarchar(max) 
select @sql = 'SELECT a.id, '
declare @maxval int 
select @maxval = max(id) from @cols
declare @c int = 1
declare @curcol varchar(25)

while @c <= @maxval
begin
    if @c > 1
        select @sql += ', '

    select @curcol = colname from @cols where id = @c
    select @sql += 'a.' + @curcol + ' as ''Table_1_' + @curcol + ''', b.' + @curcol + ' as ''Table_2_' + @curcol + ''', 
    CASE
      WHEN a.' + @curcol + ' IS NULL AND b.' + @curcol + ' IS NULL THEN ''True''  
      WHEN a.' + @curcol + ' = b.' + @curcol + ' THEN ''True''  
      ELSE ''False'' 
    END AS ''Is Equal?'''

    set @c = @c + 1
end

select @sql += 'FROM [User] a 
INNER JOIN User_Group b ON a.id = b.id'

exec sp_executesql @sql

使用以下测试数据:

create table [User] (id int, username varchar(10), fname varchar(25), lname varchar(25)) 
create table [User_Group] (id int, username varchar(10), fname varchar(25), lname varchar(25))

insert into [User] values (1, 'user1', 'fname1', 'lname1')
insert into [User_Group] values (1, 'user1', 'fname1', 'lname1')
insert into [User] values (2, 'user2', 'fname2', 'lname2')
insert into [User_Group] values (2, 'user2', 'fnameasdf', 'lnamesdfg')

它会生成此输出:

id  Table_1_username    Table_2_username    Is Equal?   Table_1_fname   Table_2_fname   Is Equal?   Table_1_lname   Table_2_lname   Is Equal?
1   user1               user1               True        fname1          fname1          True        lname1          lname1          True
2   user2               user2               True        fname2          fnameasdf       False       lname2          lnamesdfg       False

以下是它生成的查询:

SELECT a.id, a.username as 'Table_1_username', b.username as 'Table_2_username', 
    CASE
      WHEN a.username IS NULL AND b.username IS NULL THEN 'True'  
      WHEN a.username = b.username THEN 'True'  
      ELSE 'False' 
    END AS 'Is Equal?', a.fname as 'Table_1_fname', b.fname as 'Table_2_fname', 
    CASE
      WHEN a.fname IS NULL AND b.fname IS NULL THEN 'True'  
      WHEN a.fname = b.fname THEN 'True'  
      ELSE 'False' 
    END AS 'Is Equal?', a.lname as 'Table_1_lname', b.lname as 'Table_2_lname', 
    CASE
      WHEN a.lname IS NULL AND b.lname IS NULL THEN 'True'  
      WHEN a.lname = b.lname THEN 'True'  
      ELSE 'False' 
    END AS 'Is Equal?'FROM [User] a 
INNER JOIN User_Group b ON a.id = b.id