I was doing update of big table in Sybase and had following code (declarations skipped):
-- 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
So it’s usual idea as you can see: take some ordered column (id) and update rows where id is between N and M. Loop until all rows are updated.
As you might see, in order to get @max_id I used set rowcount 10000 and then select @max_id = id from (select id from table1 where id >= @min_id and further_conditions) a. Now I need to do such update on database which is running Sybase ASE 12.0. The problem is that derived tables are only supported from 12.5, so select id from (select id from table1) a is not working. Any ideas how to fix it, perhaps algorithm can be changed? Of course, I can do needed updates with the help of cursor, but I was wondering if there was some better way.
Try to change code:
to:
or