sql临时表除外

时间:2013-04-26 23:31:21

标签: sql join except temp-tables

我正在尝试将此查询的记录插入到临时表中。任何帮助或建议都会有所帮助

insert into #AddRec
select *
from stg di
right join
ED bp
on
bp.ID = di.ID
except
select *
from stg di
inner join
ED bp
on
bp.ID = di.ID 

4 个答案:

答案 0 :(得分:2)

This可能有助于简化您的查询。

create table #AddRec(id int) ;

insert into #addrec
select  ed.id
from stg right join 
ed on stg.id=ed.id 
where stg.id is null;

select * from #Addrec

如果需要表中的更多字段,请将定义添加到临时表中,并将它们添加到选择行

答案 1 :(得分:0)

如果你的select返回符合插入表的字段数,那么这应该有效!

  insert into tbl1 (field1,field2)
    select field1,field2 from.................

答案 2 :(得分:0)

;WITH Q AS
(
select *
from stg di
right join
ED bp
on
bp.ID = di.ID
except
select *
from stg di
inner join
ED bp
on
bp.ID = di.ID 
)
INSERT INTO #AddRec(... list of fields ...)
SELECT (... list of fields ...) FROM Q

如果要从头创建临时表,只需将插入替换为:

SELECT (... list of fields ...) 
INTO #AddRec
FROM Q

答案 3 :(得分:0)

最简单的方法

select *
into #AddRec
from stg di
right join ED bp on bp.ID = di.ID
except
select * from stg di
inner join ED bp on bp.ID = di.ID