0

I've seen the insert select but I only want to get one value from the second table. Here's what I'm thinking:

insert into table1
Values((select distinct id from table2),
"blah", "blah", "blah")

So after the Id, the rest of the values will be hard coded to whatever I want. Is this possible?

Pittfall
  • 2,751
  • 6
  • 32
  • 61

2 Answers2

8

The correct syntax does not use values:

insert into table1(col1, . . . )
    select distinct id, 'blah', 'blah', 'blah'
    from table2;

Notes:

  • You should always use explicit column lists when using insert, unless you really, really know what you are doing.
  • Use single quotes to delimit a string, not double quotes.
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
1

The syntax looks like this:

insert into table1
select distinct id, 'blah', 'blah', 'blah' from table2

Use single quotes for string literals, like 'blah'. (Double quotes are for delimited identifiers, e.g. if an object has an reserved word as name "table".)

jarlh
  • 42,561
  • 8
  • 45
  • 63