0

我有这段代码,当我运行它时,我得到以下信息:

 DECLARE @Consumption float = 2211,
            @BillingMonth datetime = '2012-11-01 00:00:00.000',
            @SiteName varchar(100) = 'Aldr',
            @Type int = 1


    SELECT Consumption, MeterID, SiteID
    FROM  tblMEP_Sites

    JOIN tblMEP_Meters
    ON tblMEP_Meters.SiteID = tblMEP_Sites.ID

    JOIN tblMEP_Monthlydata
    ON tblMEP_MonthlyData.MeterID = tblMEP_Meters.ID

    WHERE ProjectID = 40
    AND tblMEP_Sites.Name LIKE @SiteName

    AND Type = @Type
    AND BillingMonth = @BillingMonth

结果:

Consumption MeterID SiteID
25900   13274   1622

我想使用更新语句更新消费字段:

Update tblMEP_Monthlydata
SET Consumption= @Consumption

并且还获取 MeterID 和 SiteID,因此结果将如下所示:

Consumption MeterID SiteID
2211        13274   1622

tempTables 是解决这个问题的方法吗?如果是,我将如何将上述代码转换为临时表?

4

3 回答 3

0

 

    DECLARE @Consumption float = 2211,
            @BillingMonth datetime = '2012-11-01 00:00:00.000',
            @SiteName varchar(100) = 'Aldr',
            @Type int = 1

    UPDATE tblMEP_Monthlydata 
    set 
    Consumption = @Consumption


    FROM  tblMEP_Sites

    JOIN tblMEP_Meters
    ON tblMEP_Meters.SiteID = tblMEP_Sites.ID

    JOIN tblMEP_Monthlydata
    ON tblMEP_MonthlyData.MeterID = tblMEP_Meters.ID

    WHERE ProjectID = 40
    AND tblMEP_Sites.Name LIKE @SiteName

    AND Type = @Type
    AND BillingMonth = @BillingMonth
于 2013-07-17T18:41:17.310 回答
0
DECLARE @Results TABLE
(
    Consumption FLOAT,
    MeterID INT,
    SiteI INT
);

UPDATE  tblMEP_Monthlydata
SET     Consumption = @Consumption 
OUTPUT  inserted.Consumption, inserted.MeterID, inserted.SiteID INTO @Results
/*
-- Or, if this combination of values (ProjectID=40,Name,Type,BillingMonth) is unique (maybe you have an [unique] index view ?)
-- then you could use this syntax
SET     Consumption = @Consumption, 
        @MeterID = MeterID,
        @SiteID = SiteID
*/      
FROM    tblMEP_Sites
JOIN    tblMEP_Meters ON tblMEP_Meters.SiteID = tblMEP_Sites.ID
JOIN    tblMEP_Monthlydata ON tblMEP_MonthlyData.MeterID = tblMEP_Meters.ID
WHERE   ProjectID = 40
AND     tblMEP_Sites.Name = @SiteName
AND     Type = @Type
AND     BillingMonth = @BillingMonth

SELECT * FROM @Results;
于 2013-07-17T19:07:16.157 回答
0

你想先做一个UPDATE然后再做SELECT你的数据。

UPDATE MD
SET Consumption = @Consumption
FROM  tblMEP_Sites S
INNER JOIN tblMEP_Meters M
    ON M.SiteID = S.ID
INNER JOIN tblMEP_Monthlydata MD
    ON MD.MeterID = M.ID
WHERE ProjectID = 40                -- use the table alias as a prefix 
AND S.Name LIKE @SiteName           -- changed the LIKE with "=" because 
                                    -- they are doing the same
AND Type = @Type                    -- use the table alias as a prefix
AND BillingMonth = @BillingMonth    -- use the table alias as a prefix


SELECT Consumption, MeterID, SiteID
FROM  tblMEP_Sites S
INNER JOIN tblMEP_Meters M
    ON M.SiteID = S.ID
INNER JOIN tblMEP_Monthlydata MD
    ON MD.MeterID = M.ID
WHERE ProjectID = 40                
AND S.Name LIKE @SiteName           
AND Type = @Type                    
AND BillingMonth = @BillingMonth    
于 2013-07-17T18:50:47.857 回答