用查询将数据插入到临时表中
我有一个现有的查询输出当前的数据,我想插入到一个临时表,但我有这样做的一些问题。 有人会有一些见解如何做到这一点?
这是一个例子
SELECT * FROM (SELECT Received, Total, Answer, ( CASE WHEN application LIKE '%STUFF%' THEN 'MORESTUFF' END ) AS application FROM FirstTable WHERE Recieved = 1 AND application = 'MORESTUFF' GROUP BY CASE WHEN application LIKE '%STUFF%' THEN 'MORESTUFF' END) data WHERE application LIKE isNull('%MORESTUFF%', '%')
这似乎输出我的数据目前的方式,我需要它,但我想通过它进入临时表。 我的问题是,我是相当新的SQL查询,并没有能够find一种方法来这样做。 或者如果这是可能的。 如果这是不可能的,是否有一个更好的方法来获取我正在寻找的数据,如WHERE application LIKE isNull('%MORESTUFF%','%')
到临时表?
任何帮助将非常感激! 谢谢!
SELECT * INTO #Temp FROM (SELECT Received, Total, Answer, (CASE WHEN application LIKE '%STUFF%' THEN 'MORESTUFF' END) AS application FROM FirstTable WHERE Recieved = 1 AND application = 'MORESTUFF' GROUP BY CASE WHEN application LIKE '%STUFF%' THEN 'MORESTUFF' END) data WHERE application LIKE isNull( '%MORESTUFF%', '%')
SQL Server R2 2008需要如下的AS
子句:
SELECT * INTO #temp FROM ( SELECT col1, col2 FROM table1 ) AS x
查询失败,最后没有AS x
。
最快的方法是使用“SELECT INTO”命令
SELECT * INTO #TempTableName FROM....
这将创build一个新表,您不必提前创build它。
你可以这样做:
INSERT INTO myTable (colum1, column2) SELECT column1, column2 FROM OtherTable;
只要确保列匹配,数量和数据types一样。
尝试这个:
SELECT * INTO #Temp FROM (select * from tblorders where busidate ='2016-11-24' and locationID=12 ) as X
请使用别名与x,所以它不会失败的脚本和结果。
就我个人而言,我需要一个小小的手来搞清楚如何使用它,这真的很棒。
SELECT * INTO #TEMP FROM ( The query you want to use many times ) AS X SELECT * FROM #TEMP WHERE THIS = THAT SELECT * FROM #TEMP WHERE THIS <> THAT SELECT COL1,COL3 FROM #TEMP WHERE THIS > THAT DROP TABLE #TEMP
SELECT * INTO #TempTable FROM SampleTable WHERE... SELECT * FROM #TempTable DROP TABLE #TempTable