Subject Re: [firebird-support] Question about a subquery
Author Milan Babuskov
Paul R. Gardner wrote:
> Can anyone explain this behavior to me? I have an INVOICE table with
> 100 records. I wanted to update a maximum of 25 records.
> UPDATE INVOICE I SET I.FIELD = FOO
> WHERE I.INVOICE_ID IN (SELECT FIRST 25 I2.INVOICE_ID FROM INVOICE I2);

Please note that "select FIRST" makes no sense without ORDER BY, as it
returns "any 25 records", and since it is evaluated for each updated
row, it can easily update more than 25 records - depending on how lucky
you are.

> Is this a bug, or am I not understanding something?

Never use FIRST and SKIP without ORDER BY. You can use ROWS clause in
Firebird 2 to achieve the same more efficiently. Also, using EXISTS
instead of IN can be more efficient for older Firebird version.

However, the entire idea seems wrong. You're trying to update a 25
completely random invoices, which doesn't make much sense to me?

--
Milan Babuskov
http://www.guacosoft.com