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

前端 未结 6 925
隐瞒了意图╮
隐瞒了意图╮ 2020-11-28 05:26

I really would appreciate your help.

Probably it\'s a quite simple problem to solve - but I\'m not the one .. ;-)

I have two tables in SQL Server:

相关标签:
6条回答
  • 2020-11-28 05:52
    INSERT INTO prices (group, id, price)
      SELECT 7, articleId, 1.50 FROM article WHERE name LIKE 'ABC%'
    
    0 讨论(0)
  • 2020-11-28 05:52

    If you are inserting one record into your table, you can do

    INSERT INTO yourTable 
    VALUES(value1, value2)
    

    But since you want to insert more than one record, you can use a SELECT FROM in your SQL statement.

    so you will want to do this:

    INSERT INTO prices (group, id, price) 
    SELECT 7, articleId, 1.50
    from article 
    WHERE name LIKE 'ABC%'
    
    0 讨论(0)
  • 2020-11-28 05:56

    Try this:

    INSERT INTO prices (
        group, 
        id,
        price
    ) 
    SELECT
        7,
        articleId,
        1.50
    FROM
        article 
    WHERE 
        name LIKE 'ABC%';
    
    0 讨论(0)
  • 2020-11-28 06:03

    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.

    0 讨论(0)
  • 2020-11-28 06:03

    the sub query looks like

     insert into table_name (col1,col2,....) values (select col1,col2,... FROM table_2 ...)
    

    hope this help

    0 讨论(0)
  • 2020-11-28 06:11
    INSERT INTO prices(group, id, price)
    SELECT 7, articleId, 1.50
    FROM article where name like 'ABC%';
    
    0 讨论(0)
提交回复
热议问题