13

我有这个查询:

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 中创建临时表的语法,那就太好了。

我尝试了不同的方法,但我迷路了,没有得到我想要的结果。

4

6 回答 6

30

如果您只想在查询中创建一个临时表,它允许您对存入其中的结果执行某些操作,您可以执行以下操作:

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

在查询的顶部,然后执行

INSERT INTO @T1
SELECT
FROM
(...)
于 2013-07-11T18:08:23.343 回答
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
于 2013-07-11T18:09:50.327 回答
6

如果你想在同一个查询中从临时表中查询结果,你可以使用#temp 表,或者@table variables(我个人更喜欢@),在你想要使用## global temp 的范围之外进行查询表或使用结果创建一个新表。

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
于 2013-07-11T18:05:40.050 回答
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
@- 使用它来创建表作为变量。

于 2013-07-11T18:06:53.087 回答
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.......
于 2017-04-07T08:20:28.483 回答
0

如果要在检查存在表后创建临时表。您可以使用以下代码

DROP TABLE IF EXISTS tempdb.dbo.#temptable
CREATE TABLE #temptable
  (
   SiteName             NVARCHAR(50),
   BillingMonth         varchar(10),
   Consumption          INT,
  )

创建临时表后,您可以将数据作为常规表插入到该表中:

INSERT INTO #temptable
SELECT COLUMN1,...
FROM
(...)

或者

INSERT INTO #temptable
VALUES (value1, value2, value3, ...);

SELECT 语句用于从临时表中选择数据。

SELECT * FROM #temptable

您可以使用 DROP TABLE 语句手动删除临时表:

DROP TABLE #temptable;
于 2019-10-13T07:57:37.940 回答