1

我正在更新 Sybase 中的大表并有以下代码(已跳过声明):

-- get minimum id 
set rowcount 1 
select @min_id = id from table1 where id matches_some_condition

set rowcount 10000 -- batch size

select @max_id = id from 
(select id from table1 where id >= @min_id and some_condition) a order by id

-- do updates
while (1 = 1)
 begin
   update table1
   set a = some_value
   where id >= @min_id and id <= @max_id and further_conditions

   waitfor delay '00:00:01'

   select @min_id = @max_id
   select @max_id = id from 
    (select id from table1 where id >= @min_id and further_conditions
    ) a order by id

   if @min_id >= @max_id
      break

 end

因此,如您所见,这是通常的想法:获取一些有序列 (id) 并更新 id 介于 N 和 M 之间的行。循环直到所有行都更新。

如您所见,为了获得 @max_id 我使用了set rowcount 10000然后select @max_id = id from (select id from table1 where id >= @min_id and further_conditions) a. 现在我需要对运行 Sybase ASE 12.0 的数据库进行此类更新。问题是仅从 12.5 开始支持派生表,因此select id from (select id from table1) a无法正常工作。任何想法如何解决它,也许算法可以改变?当然,我可以在光标的帮助下进行所需的更新,但我想知道是否有更好的方法。

4

1 回答 1

1

尝试更改代码:

 select @max_id = id from 
    (select id from table1 where id >= @min_id and further_conditions
    ) a order by id

到:

 select @max_id = max(id) from 
 from table1 
 where id >= @min_id and further_conditions

或者

 select @max_id =  id 
 from table1 
 where id >= @min_id 
 and further_conditions
 order by id
于 2012-12-14T08:37:58.580 回答