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

如何把union 得到的结果放到临时表中去
(
  select *from table1
  union all
  select *from table2
  )
  into #temp3....................这样的语句应该怎么写

  也就是说如何把union 得到的结果放到临时表中去

------解决方案--------------------
SQL code
select * into # from ( 
    select *from table1 
    union all 
    select *from table2 
    ) t

------解决方案--------------------
SQL code
CREATE TABLE #A (ID INT,   AMT    INT,        S_DATE VARCHAR(10))
INSERT #A
SELECT 1,  3000,  '2008-11-21' UNION ALL 
SELECT 1,  5000,  '2008-11-19' UNION ALL 
SELECT 2,  4000,  '2008-11-17' UNION ALL 
SELECT 2,  6000,  '2008-11-19' UNION ALL 
SELECT 3,  1000,  '2008-10-12' UNION ALL 
SELECT 3,  200,  '2008-11-02'

CREATE TABLE #B (ID INT,   AMT    INT,        S_DATE VARCHAR(10))
INSERT #B
SELECT 1,  3000,  '2008-11-21' UNION ALL 
SELECT 1,  5000,  '2008-11-19' UNION ALL 
SELECT 2,  4000,  '2008-11-17' UNION ALL 
SELECT 2,  6000,  '2008-11-19' UNION ALL 
SELECT 3,  1000,  '2008-10-12' UNION ALL 
SELECT 3,  200,  '2008-11-02'


SELECT * INTO #C  FROM 
(SELECT * FROM #A
UNION ALL
SELECT * FROM #B)A

SELECT * FROM #C
DROP TABLE #A,#B

/**
ID          AMT         S_DATE     
----------- ----------- ---------- 
1           3000        2008-11-21
1           5000        2008-11-19
2           4000        2008-11-17
2           6000        2008-11-19
3           1000        2008-10-12
3           200         2008-11-02
1           3000        2008-11-21
1           5000        2008-11-19
2           4000        2008-11-17
2           6000        2008-11-19
3           1000        2008-10-12
3           200         2008-11-02