在 Update 语句中使用表别名的好方法?

使用 SqlServer,并尝试从同一个表中更新行。我想使用一个表别名来提高可读性。 我现在就是这么做的:

UPDATE ra
SET ra.ItemValue = rb.ItemValue
FROM dbo.Rates ra, dbo.Rates rb
WHERE ra.ResourceID = rb.ResourceID
AND ra.PriceSched = 't8'
AND rb.PriceSched = 't9'

有更容易/更好的方法吗?

76287 次浏览
UPDATE ra
SET ra.ItemValue = rb.ItemValue
FROM dbo.Rates ra
INNER JOIN dbo.Rates rb
ON ra.ResourceID = rb.ResourceID
WHERE ra.PriceSched = 't8'
AND rb.PriceSched = 't9';

This might help in improving performance.

Table alias in Update Query in T-SQL( Microsoft SQL) . for MS SQL Server 2008 R2 it's work just fine

UPDATE A_GeneralLedger  set ScheduleId=g.ScheduleId
from A_GeneralLedger l inner join A_AcGroup g on g.ACGroupID=l.AccountGroupID

Use the following code :

UPDATE ra
SET ra.ItemValue = rb.ItemValue
FROM dbo.Rates ra, dbo.Rates rb
WHERE ra.ResourceID = rb.ResourceID
AND ra.PriceSched = 't8'
AND rb.PriceSched = 't9'