2013-07-17 106 views
0

我有這樣的代碼,當我運行它,我得到以下幾點:SQL Server的臨時表更新字段

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是這個問題的解決方案嗎?如果是,我將如何將上面的代碼轉換爲臨時表?

+1

目前我們不知道你的'ConsumptionTable' releates任何其他表如何 – Lamak

+0

對不起!這是tblMEP_Monthlydata –

回答

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 
+0

我不認爲更新該表中的所有行是想要的運算。 – Lamak

+0

無明顯但它可以讓他們在正確的directon – Gratzy

+0

不是真的,你剛纔添加已經提供'UPDATE'的代碼。我認爲,如果你想點他/她在朝着正確的方向,而無需等待澄清,你可能已經至少*承擔*'上一個ConsumptionTable'欄上的'JOIN'使用有 – Lamak

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  
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;