我有一个输出当前数据的现有查询,我想将它插入Temp表中,但是我遇到了一些问题。有人会对如何做到这一点有所了解吗?
这是一个例子
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查询很新,并且无法找到一种方法。或者,如果它是可能的。如果不可能,是否有更好的方法将我正在寻找WHERE application LIKE isNull('%MORESTUFF%','%')
的数据放入临时表?
任何帮助将不胜感激!谢谢!
答案 0 :(得分:150)
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%',
'%')
答案 1 :(得分:29)
最快的方法是使用“SELECT INTO”命令,例如
SELECT * INTO #TempTableName
FROM....
这将创建一个新表,您无需事先创建它。
答案 2 :(得分:8)
你可以这样做:
INSERT INTO myTable (colum1, column2)
SELECT column1, column2 FROM OtherTable;
确保列匹配,数量与数据类型相同。
答案 3 :(得分:5)
就我个人而言,我需要一只手拿着弄清楚如何使用它,这真的很棒。
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
答案 4 :(得分:3)
试试这个:
SELECT *
INTO #Temp
FROM
(select * from tblorders where busidate ='2016-11-24' and locationID=12
) as X
请使用x的别名,以免脚本和结果失败。
答案 5 :(得分:1)
SELECT * INTO #TempTable
FROM SampleTable
WHERE...
SELECT * FROM #TempTable
DROP TABLE #TempTable
答案 6 :(得分:1)
这是可能的。 试试这个:
Create Global Temporary Table
BossaDoSamba
On Commit Preserve Rows
As
select ArtistName, sum(Songs) As NumberOfSongs
from Spotfy
where ArtistName = 'BossaDoSamba'
group by ArtistName;
答案 7 :(得分:0)
在查询结束时使用
选择*到#temp (select * from table1,table2) 作为 temp_table