日期:2014-05-18  浏览次数:20732 次

sql,两个表中的数据对比
查询两个表,比较他们没列的值是否相同,如果不相同,将不相同的插入到另以个临时表中.

因为我要比较很多表,所以希望可以传表名.

这个要用sql写出来。

------解决方案--------------------
SQL code
insert into temp select * from a except select * from b
insert into temp select * from b except select * from a

------解决方案--------------------
SQL code
id name age 
2 null 18

------解决方案--------------------
探讨

引用:

SQL code
insert into temp select * from a except select * from b
insert into temp select * from b except select * from a
这样会取出不同的吗?

------解决方案--------------------
SQL code
--A,B 两个表
--A:
--id name age  
--1 张三 22
--2 张四 21
--B:
--id name age  
--1 张三 22
--2 张四 18

--那么得到的结果应该是
--在一个临时表中插入:
--id name age  
--2 null 18
if OBJECT_ID('temp')is not null 
drop table temp
go 
create table temp ( id int ,name varchar(50), age int )
if OBJECT_ID('A')is not null 
drop table A
go 
create table A ( id int ,name varchar(50), age int )
insert into A select 1 ,'张三', 22 union all 
select 2, '张四' ,21  
 
if OBJECT_ID('B')is not null 
drop table B
go 
create table B ( id int ,name varchar(50), age int )
insert into B select 1 ,'张三', 22 union all 
select 2, '张四' ,18  
insert into  temp select * from A except select * from B
select * from temp



id          name                                               age
----------- -------------------------------------------------- -----------
2           张四                                                 21

(1 行受影响)

------解决方案--------------------
去重
SQL code
insert into temp
select * from a except select * from b
union
select * from b except select * from b

------解决方案--------------------
SQL code

--又分的幺妹
--方法1
select * into #t from a where 1<>1
insert into #t select * from a except select * from b
insert into #t select * from b except select * from a
--方法2
select * into #t from a where 1<>1
SELECT * FROM 
(SELECT * FROM aaa UNION SELECT * FROM dbo.sc )AS a EXCEPT
(select * from aaa INTERSECT SELECT * FROM dbo.sc)

--方法还有很多种... 集合问题

------解决方案--------------------
SQL code
--A,B 两个表
--A:
--id name age  
--1 张三 22
--2 张四 21
--B:
--id name age  
--1 张三 22
--2 张四 18

--那么得到的结果应该是
--在一个临时表中插入:
--id name age  
--2 null 18
if OBJECT_ID('temp')is not null 
drop table temp
go 
create table temp ( id int ,name varchar(50), age int )
if OBJECT_ID('A')is not null 
drop table A
go 
create table A ( id int ,name varchar(50), age int )
insert into A select 1 ,'张三', 22 union all 
select 2, '张四' ,21  


if OBJECT_ID('B')is not null 
drop table B
go 
create table B ( id int ,name varchar(50), age int )
insert into B select 1 ,'张三', 22 union all 
select 2, '张四' ,18  
insert into  temp   select  id ,case when  name IN(select name from B ) then null else name end as 'name' ,age from 
 (select * from A except select * from B )s
 


select * from temp
 



id          name                                               age
----------- -------------------------------------------------- -----------
2           NULL                                               21

(1 行受影响)

------解决方案--------------------
SQL code
--A,B 两个表
--A:
--id name age  
--1 张三 22
--2 张四 21
--B:
--id name age  
--1 张三 22
--2 张四 18

--那么得到的结果应该是
--在一个临时表中插入:
--id name age  
--2 null 18
if OBJECT_ID('temp')is not null 
drop table temp
go 
create table temp ( id int ,name varchar(50), age int )
if OBJECT_ID('A')is not null 
drop table A
go 
create table A ( id int ,name varchar(50), age int )
insert into A select 1 ,'张三', 22 union all 
select 2, '张四' ,21