0

我有这段代码,我知道这不是完成这项工作的最有效方法,但我能说什么!我是 SQL 新手,我一行一行地做所有事情。基本上我正在更新表中的特定字段。我需要找到总更新,因为你看到列表太长而且我还有更多,所以我知道需要知道我有多少总更新。有谁知道我是否可以在 SQL 中做到这一点,而不仅仅是将代码复制并粘贴到 word 文档中并计算单词Update的数量。临时表可以做这样的事情吗?

UPDATE tblMEP_MonthlyData   
    SET Consumption = 51634

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 Consumption != 51634
AND tblMEP_Sites.Name LIKE '%Altgeld%'
AND Type = 1
AND BillingMonth = '2012-11-01 00:00:00.000'

-----------------------------------------------------------------------------

UPDATE tblMEP_MonthlyData   
    SET Consumption = 38370

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 '%Altgeld%'
AND Consumption != 38370
AND Type = 1
AND BillingMonth = '2012-10-01 00:00:00.000'

-----------------------------------------------------------------------------

UPDATE tblMEP_MonthlyData   
    SET Consumption = 108610


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 '%Avond%'
AND Consumption != 108610

AND Type = 1
AND BillingMonth = '2012-8-01 00:00:00.000'

-----------------------------------------------------------------------------

UPDATE tblMEP_MonthlyData   
    SET Consumption = 107923

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 '%Avond%'
AND Consumption != 107923

AND Type = 1
AND BillingMonth = '2012-9-01 00:00:00.000'


---------------------------------------------------------------------------------
UPDATE tblMEP_MonthlyData   
    SET Consumption = 1442

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 '%Belmont-cragin Pre-k%'
AND Consumption != 1442

AND Type = 1
AND BillingMonth = '2012-7-01 00:00:00.000'

-----------------------------------------------------------------------------


UPDATE tblMEP_MonthlyData   
    SET Consumption = 1477


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 '%Belmont%'
AND Consumption != 1477


AND Type = 1
AND BillingMonth = '2012-8-01 00:00:00.000'

-----------------------------------------------------------------------------


UPDATE tblMEP_MonthlyData   
    SET Consumption = 1636

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 '%Belmont%'
AND Consumption != 1636

AND Type = 1
AND BillingMonth = '2012-9-01 00:00:00.000'

-----------------------------------------------------------------------------


UPDATE tblMEP_MonthlyData   
    SET Consumption = 1451


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 '%Belmont%'
AND Consumption != 1451

AND Type = 1
AND BillingMonth = '2012-10-01 00:00:00.000'

-----------------------------------------------------------------------------


UPDATE tblMEP_MonthlyData   
    SET Consumption = 1615

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 '%Belmont%'
AND Consumption != 1615

AND Type = 1
AND BillingMonth = '2012-11-01 00:00:00.000'
4

3 回答 3

3

你想要的是@@ROWCOUNT.

在每个之后update,将其设置为已更新的行数。(见这里。)

就将其放入您的代码而言,您可能会执行以下操作:

declare @TotalRowsint = 0;

update . . .

set @TotalRows= @TotalRows+ @@ROWCOUNT;

update . . .

set @TotalRows= @TotalRows+ @@ROWCOUNT;

然后你可以选择总数。如果您想要每次更新的计数,则可以print @@ROWCOUNT改用。或者,将值插入表中。

使用@@ROWCOUNT 时要小心,因为大多数SQL 语句都会影响它。因此,安全的做法是立即将其分配给另一个变量,然后使用该变量:

declare @TotalRows int = 0, @NumRows int;

update . . .

set @NumRows = @@ROWCOUNT;
set @TotalRows= @TotalRows + @NumRows;
print @NumRows;
insert into AllMyUpdates(which, numrows)
    select 'This update', @NumRows;

update . . .
于 2013-07-17T16:25:25.917 回答
1

@@ROWCOUNT您可以使用变量收集受每个语句影响的行数。如果您想将其存储在任何地方,您可以在每个代码块之后添加以下行:

set @RowsAffected=@@ROWCOUNT
insert into MyTableWithTOtals (RowsAffected) values (@RowsAffected)

当然,您应该@RowsAffected在开始时声明变量。使用这种方式,您还可以计算行数和更新语句的数量。

于 2013-07-17T16:28:45.180 回答
0

请用

select @@Rowcount

在更新语句的末尾。

于 2013-07-17T16:26:05.640 回答