2013-07-11 92 views
10

我有這個疑問:SQL Server中創建一個臨時表,這個查詢

DECLARE 
@ProjectID int = 3, 
@Year int = 2010, 
@MeterTypeID int = 1, 
@StartDate datetime, 
@EndDate datetime 

SET @StartDate = '07/01/' + CAST(@Year as VARCHAR) 
SET @EndDate = '06/30/' + CAST(@Year+1 as VARCHAR) 

SELECT tblMEP_Sites.Name AS SiteName, convert(varchar(10),BillingMonth ,101) AS BillingMonth, SUM(Consumption) AS Consumption 
FROM tblMEP_Projects 

JOIN tblMEP_Sites 
ON tblMEP_Projects.ID = tblMEP_Sites.ProjectID 

JOIN tblMEP_Meters 
ON tblMEP_Meters.SiteID = tblMEP_Sites.ID 

JOIN tblMEP_MonthlyData 
ON tblMEP_MonthlyData.MeterID = tblMEP_Meters.ID 

JOIN tblMEP_CustomerAccounts 
ON tblMEP_CustomerAccounts.ID = tblMEP_Meters.CustomerAccountID 

JOIN tblMEP_UtilityCompanies 
ON tblMEP_UtilityCompanies.ID = tblMEP_CustomerAccounts.UtilityCompanyID 

JOIN tblMEP_MeterTypes 
ON tblMEP_UtilityCompanies.UtilityTypeID = tblMEP_MeterTypes.ID 

WHERE tblMEP_Projects.ID = @ProjectID 
AND tblMEP_MonthlyData.BillingMonth Between @StartDate AND @EndDate 
AND tbLMEP_MeterTypes.ID = @MeterTypeID 
GROUP BY BillingMonth, tblMEP_Sites.Name 
ORDER BY month(BillingMonth) 

我只是想將其存儲在一個臨時表中,這樣我可以用它做什麼。如果任何人都可以包含用於在SQL Server中創建臨時表的語法,那將會很棒。

我嘗試過不同的方式,但我迷路了,沒有得到我想要的結果。

+0

嘗試SELECT INTO語句 – maSTAShuFu

+0

這是自動插入到不是Temptable而不實際創建臨時表結構。你的選擇。 – maSTAShuFu

回答

24

如果你只想創建查詢裏面一個臨時表,將允許你這樣做的結果的東西,你存入它,你可以這樣做以下:

DECLARE @T1 TABLE (
Item 1 VARCHAR(200) 
, Item 2 VARCHAR(200) 
, ... 
, Item n VARCHAR(500) 
) 

在頂部您的查詢,然後做一個

INSERT INTO @T1 
SELECT 
FROM 
(...) 
+0

更正@對#,但對tempdb不正確,臨時表和表變量都存儲在tempdb中。這個表變量的神話已經持續了多年,但事實根本不是這樣。谷歌幾個有信譽的網站,都絕對寫入磁盤。 –

+1

我從來沒有說過它或沒有... –

+1

@TomStickel - 現在不用了,現在可以創建內存表以獲得更好的性能,而無需寫入磁盤:https://docs.microsoft.com/en-us/ SQL /關係的數據庫/存儲器內-OLTP /更快-TEMP-表和表可變逐使用存儲器優化。 – Ian

5

如果要查詢來自同一查詢中臨時表的結果,可以使用#temp表或@table變量(我個人比較喜歡@)來查詢您要使用的範圍外部##全局臨時表或使用結果創建一個新表。

DECLARE 
@ProjectID int = 3, 
@Year int = 2010, 
@MeterTypeID int = 1, 
@StartDate datetime, 
@EndDate datetime 

SET @StartDate = '07/01/' + CAST(@Year as VARCHAR) 
SET @EndDate = '06/30/' + CAST(@Year+1 as VARCHAR) 

DECLARE @MyTempTable TABLE (SiteName varchar(50), BillingMonth varchar(10), Consumption float) 

INSERT INTO @MyTempTable (SiteName, BillingMonth, Consumption) 
SELECT tblMEP_Sites.Name AS SiteName, convert(varchar(10),BillingMonth ,101) AS BillingMonth, SUM(Consumption) AS Consumption 
FROM tblMEP_Projects 
+1

'@ MyTempTable'會創建變量,'''會在'tempdb'中創建臨時表 – Paritosh

+0

@Paritosh如果OP不知道臨時表的語法,我認爲這個問題意味着'我需要臨時將結果存儲在我可以查詢的表結構中(在這種情況下,#或@將起作用) –

4
DECLARE #MyTempTable TABLE (SiteName varchar(50), BillingMonth varchar(10), Consumption float) 

INSERT INTO #MyTempTable (SiteName, BillingMonth, Consumption) 
SELECT tblMEP_Sites.Name AS SiteName, convert(varchar(10),BillingMonth ,101) AS BillingMonth, SUM(Consumption) AS Consumption 
FROM tblMEP_Projects....... --your joining statements 

這裏,# - 用它來創建內部tempdb
@表 - 用它來創建表作爲變量。

9

像這樣。確保刪除臨時表(在代碼塊結束時,在完成之後),否則將在隨後的運行中出錯。

SELECT 
    tblMEP_Sites.Name AS SiteName, 
    convert(varchar(10),BillingMonth ,101) AS BillingMonth, 
    SUM(Consumption) AS Consumption 
INTO 
    #MyTempTable 
FROM 
    tblMEP_Projects 
    JOIN tblMEP_Sites 
     ON tblMEP_Projects.ID = tblMEP_Sites.ProjectID 
    JOIN tblMEP_Meters 
     ON tblMEP_Meters.SiteID = tblMEP_Sites.ID 
    JOIN tblMEP_MonthlyData 
     ON tblMEP_MonthlyData.MeterID = tblMEP_Meters.ID 
    JOIN tblMEP_CustomerAccounts 
     ON tblMEP_CustomerAccounts.ID = tblMEP_Meters.CustomerAccountID 
    JOIN tblMEP_UtilityCompanies 
     ON tblMEP_UtilityCompanies.ID = tblMEP_CustomerAccounts.UtilityCompanyID 
    JOIN tblMEP_MeterTypes 
     ON tblMEP_UtilityCompanies.UtilityTypeID = tblMEP_MeterTypes.ID 
WHERE 
    tblMEP_Projects.ID = @ProjectID 
    AND tblMEP_MonthlyData.BillingMonth Between @StartDate AND @EndDate 
    AND tbLMEP_MeterTypes.ID = @MeterTypeID 
GROUP BY 
    BillingMonth, tblMEP_Sites.Name 

DROP TABLE #MyTempTable 
0
IF OBJECT_ID('tempdb..#MyTempTable') IS NOT NULL DROP TABLE #MyTempTable 

CREATE TABLE #MyTempTable (SiteName varchar(50), BillingMonth varchar(10), Consumption float) 

INSERT INTO #MyTempTable (SiteName, BillingMonth, Consumption) 
SELECT tblMEP_Sites.Name AS SiteName, convert(varchar(10),BillingMonth ,101) 
AS BillingMonth, SUM(Consumption) AS Consumption 
FROM tblMEP_Projects....... 
相關問題