我有這個疑問: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中創建臨時表的語法,那將會很棒。
我嘗試過不同的方式,但我迷路了,沒有得到我想要的結果。
嘗試SELECT INTO語句 – maSTAShuFu
這是自動插入到不是Temptable而不實際創建臨時表結構。你的選擇。 – maSTAShuFu