Welliam Welliam - 3 months ago 17
SQL Question

Firebird sql to insert a typical record from another table with only one different field

I work on Firebird 2.5
and I have two tables, all their columns are similar except one has a primary key with auto increment and a not null foreign key field (A) for master table

I know I can use sql like this to insert all values from the two tables

insert into table1 select * from table2 where somthing = 'foo'


but what about the field (A) is there any way to insert this value manually in the same sql statement ? as this is the only field need to be entered manually

Thanks

Answer

You can specify both the source and target fields explicitly (and you should; don't use select * unless you have a specific reason to):

insert into table1
(
    col1,
    col2,
    col3,
    col4
)
select
    col1,
    col2,
    col3,
    'foo'

from table2

where something = 'foo'