在SQL Server中使用#temptable存在条件的所有联合?

时间:2015-05-18 14:32:15

标签: sql-server tsql sql-server-2008-r2

我得到了

INSERT INTO TABLE3
SELECT * FROM table1
UNION ALL
if object_id('tempdb..#temp') is not null drop table tempdb..#temp
select * into #temp from (SELECT * FROM table2) then original SELECT for UNION

但我收到语法错误?

任何帮助将不胜感激。谢谢

1 个答案:

答案 0 :(得分:3)

我不确定你想要达到的目标,但我认为它会帮助你在插入之前删除表格。

if object_id('tempdb..#temp') is not null 
    drop table tempdb..#temp

SELECT * INTO #temp
FROM table2

INSERT INTO TABLE3
SELECT *
FROM table1
UNION ALL
SELECT *
FROM #temp