Subject Re: [firebird-support] Copying rows into tables
Author Milan Babuskov
yaedos2000 wrote:
> INSERT INTO TABLE_2 SELECT * FROM TABLE_1 WHERE ID = :SOMETHING;
>
> This works fine if ID is a primary key, as only one result is
> returned. However, ID isn't necessarily unique and there could be
> multiple rows returned. How could the above statement be re-written
> to take account of this?

And how would you decide *which* row do you want if there are multiple
rows with same ID = :something?

If you have multiple rows that are the same, you could write:

insert into table_2
select first 1 * from table_1 where id = :something;

Or (since you're inside SP), declare variables for each column, fetch
one row into them, and use as insert:

declare variable field1 ...
declare variable field2 ...
etc.

select field1, field2, ...
from table_1 where id = :something
into :field1, :field2, ...;

insert into table2(field1, field2,...) values (:field1, :field2...);

HTH
--
Milan Babuskov
http://fbexport.sourceforge.net
http://www.flamerobin.org