SQL Server 为此查询创建临时表 [英] SQL Server Creating a temp table for this query
本文介绍了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 中创建临时表的语法,那就太好了.
I just want store it in a temp table so that I can do something with it. It would be great if anybody can just include the syntax for creating a temp table in SQL Server.
我尝试了不同的方法,但我迷路了,没有得到我想要的结果.
I tried different ways but I was lost and did not get the result I want.
推荐答案
如果您只想在查询中创建一个临时表,以允许您对存入其中的结果执行某些操作,您可以执行以下操作以下:
If you want to just create a temp table inside the query that will allow you to do something with the results that you deposit into it you can do something like the following:
DECLARE @T1 TABLE (
Item 1 VARCHAR(200)
, Item 2 VARCHAR(200)
, ...
, Item n VARCHAR(500)
)
在查询的顶部,然后执行
On the top of your query and then do an
INSERT INTO @T1
SELECT
FROM
(...)
这篇关于SQL Server 为此查询创建临时表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文