2009-04-19 166 views
0

使用subquerys我有一個觸發以下SQL語句刪除對火災:在Update語句

UPDATE bk2_InfoPages 
SET SortOrder = SortOrder - (SELECT COUNT(*) FROM Deleted d WHERE d.SortOrder <= SortOrder) 

我的問題是,最後SortOrderDeleted桌上,而不是到bk2_InfoPages表。我不允許在bk2_InfoPages表中添加別名,因爲它是UPDATE聲明 - 所以我應該怎麼做?

回答

1

這應該工作:

UPDATE b 
SET SortOrder = SortOrder - 
    (SELECT COUNT(*) FROM Deleted d WHERE d.SortOrder <= b.SortOrder) 
from bk2_InfoPages b 

你有別名的表做子查詢,例如:

-- this executes fine 
create table #t (t int) 

update t 
set t = (select count(*) from #t t1 where t.t = t1.t) 
from #t t 
+0

這個伎倆!謝謝! =) – 2009-04-19 02:03:11

2
UPDATE bk2_InfoPages 
SET SortOrder = SortOrder - (SELECT COUNT(*) FROM Deleted d WHERE d.SortOrder <= bk2_InfoPages.SortOrder) 
+0

不幸的是我試圖執行時出現以下錯誤信息ALTER TRIGGER語句:「多部分標識符」bk2_InfoPages.SortOrder「無法綁定。」 – 2009-04-19 01:39:19