日期:2014-05-18 浏览次数:20898 次
insert into temp select * from a except select * from b insert into temp select * from b except select * from a
------解决方案--------------------
id name age 2 null 18
------解决方案--------------------
--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 行受影响)
------解决方案--------------------
去重
insert into temp select * from a except select * from b union select * from b except select * from b
------解决方案--------------------
--又分的幺妹 --方法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) --方法还有很多种... 集合问题
------解决方案--------------------
--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 行受影响)
------解决方案--------------------
--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