Using insert into ... select results in a incorrect syntax near select, why?

You don't need the values key word and also you can add the default value 2 for b_id column in the select list instead of adding it after the SELECT statement

Try this:

INSERT INTO tableX (a_id, b_id) 
SELECT service_id, 2 
  FROM tableY 
 WHERE id = 10

While my original answer gave a working solution, I was actually wrong about the cause of the error. There is nothing wrong with using a scalar subquery inside a VALUES clause. The problem with the statement in the question is simply that one parenthesis is in the wrong place; the scalar subquery must be enclosed in parentheses.

This should work:

insert into tableX (a_id, b_id) 
VALUES (
  (SELECT service_id 
         FROM tableY 
         WHERE id = 10)
  , 2
  );

Original Answer

VALUES can only be used in conjunction with literal values. However, literal values can be used in a subquery. Do this:

insert into tableX (a_id, b_id) SELECT service_id, 2 FROM tableY WHERE id = 10

Tags:

Sql