【问题标题】:How can I make this an insert statement我怎样才能使它成为一个插入语句
【发布时间】:2018-04-30 21:01:01
【问题描述】:

我是 SQL 新手,所以我有一个名为“DealerShip”的表,并且有许多不同的汽车 ID。我有一家名为“Hondo”的经销店和另一家名为“Mitch”的经销店。我想在“Hondo”拥有的“Mitch”中插入大约 80 条记录。例如这个查询

select * from DealerShips where name='Hondo' and CarType=63

上面的查询包含大约 70 条记录,我如何创建一个插入语句来插入上面该查询的所有返回记录?除了名称将是 'Mitch' 之外,插入将进入上面的同一个表。我正在使用 MSSQL 2012

【问题讨论】:

    标签: sql sql-server-2012


    【解决方案1】:

    INSERT INTO SELECT

    INSERT INTO yourtable (FIELDS...)  ---- fields here should match the select fields
    SELECT FIELDS... FROM
    DealerShips where name = 'Hondo' and CarType=63
    

    【讨论】:

      【解决方案2】:

      只需确保以相同的顺序列出插入和选择中的列。

      INSERT INTO DealerShips (name, cartype, more columns)
      SELECT 
          'Mitch'
        , cartype
        , more columns
      from DealerShips where name='Hondo' and CarType=63
      

      【讨论】:

        猜你喜欢
        • 2011-11-17
        • 1970-01-01
        • 1970-01-01
        • 2018-05-03
        • 1970-01-01
        • 1970-01-01
        • 2023-03-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多