使用独立的Sql Query结果填充临时表

时间:2012-03-15 08:28:58

标签: sql sql-server-2005

我想从stored procedure返回一个临时表,该表填充从两个独立的sql查询中检索到的数据

Select column1,column2 FROM TABLE1 WHERE someCondition
Select column3,column4 FROM TABLE1 WHERE someOtherCondition
INSERT INTO Temp_table(column1,column2,column3,column4) values VALUE from those two table

表中的一些结果也包含null。我也在某些列上使用了sum之类的数学函数 提前致谢

2 个答案:

答案 0 :(得分:2)

尝试使用以下代码:

INSERT INTO Temp_table (column1, column2, column3, column4)
SELECT column1, column2, ISNULL(column3,0), ISNULL(column4,0) FROM TABLE1 WHERE someCondition
UNION ALL
SELECT ISNULL(column1,0), ISNULL(column2,0), column3, column4 FROM TABLE1 WHERE someOtherCondition

答案 1 :(得分:0)

你想做类似的事情:

INSERT INTO Temp_table (column1, column2, column3, column4)
SELECT column1, column2, NULL AS column3, NULL AS column4 FROM TABLE1 WHERE someCondition
UNION
SELECT NULL AS column1, NULL AS column2, column3, column4 FROM TABLE1 WHERE someOtherCondition