Search code examples
sqlinsertsubquery

How can I insert values into a table, using a subquery with more than one result?


I have two tables in SQL Server:

  1. article
  2. prices

Now I want to select a certain set of ids and insert some entries into the prices-table with those ID.

e.g. (wrong and not working SQL)

INSERT INTO prices (group, id, price) 
VALUES (7, (select articleId from article WHERE name LIKE 'ABC%'), 1.50);

SQL Error -> subquery has more than 1 value


Solution

  • You want:

    insert into prices (group, id, price)
    select 
        7, articleId, 1.50
    from article where name like 'ABC%';
    

    where you just hardcode the constant fields.