0

我在删除时触发的触发器中有以下 SQL 语句:

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

我的问题是最后一个SortOrder是指Deleted表格而不是bk2_InfoPages表格。我不允许向bk2_InfoPages表中添加别名,因为它是一个UPDATE语句 - 那么我应该怎么做呢?

4

2 回答 2

2
UPDATE bk2_InfoPages
SET SortOrder = SortOrder - (SELECT COUNT(*) FROM Deleted d WHERE d.SortOrder <= bk2_InfoPages.SortOrder)
于 2009-04-19T01:29:44.830 回答
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
于 2009-04-19T01:47:08.673 回答