Hi,
I tried using a unique key in this way, and it doesn't work. DB2 balks at the "fetch first 50 rows only" stanza in the sub-query.
echo create the test table and fill it with some test data
create table test (pk bigint not null, status bigint not null, data bigint not null);
alter table test add primary key (pk);
insert into test select row_number() over(), 0, 0 from sysibm.systables;
echo set the data to the pk value
update test set data=pk;
This statement works:
update test
set status=1, data=0
where pk in (select pk
from test
where status=0)
This statement doesn't work:
update test
set status=1, data=0
where pk in (select pk
from test
where status=0
fetch first 5 rows only)
I get the following:
SQL0104N An unexpected token "update test set status=1, data=0 where pk " was found following "BEGIN-OF-STATEMENT". Expected tokens may include:
"<values>". SQLSTATE=42601
I tested the sub-query on its own and it works.
select pk
from test
where status=0
fetch first 5 rows only
I was able to do this:
update test set status=1, data=0
where test.pk in (select test.pk
from test
inner join (select row_number() over () as RN, pk
from test ) b on test.pk=b.pk and b.RN <= 5)
The question is, how bad is this on performance?
Thanks,
Lior