Insert into … values ( SELECT … FROM … )

前端 未结 26 2423
我在风中等你
我在风中等你 2020-11-21 05:40

I am trying to INSERT INTO a table using the input from another table. Although this is entirely feasible for many database engines, I always seem to struggle t

26条回答
  •  我在风中等你
    2020-11-21 06:20

    select *
    into tmp
    from orders
    

    Looks nice, but works only if tmp doesn't exists (creates it and fills). (SQL sever)

    To insert into existing tmp table:

    set identity_insert tmp on
    
    insert tmp 
    ([OrderID]
          ,[CustomerID]
          ,[EmployeeID]
          ,[OrderDate]
          ,[RequiredDate]
          ,[ShippedDate]
          ,[ShipVia]
          ,[Freight]
          ,[ShipName]
          ,[ShipAddress]
          ,[ShipCity]
          ,[ShipRegion]
          ,[ShipPostalCode]
          ,[ShipCountry] )
          select * from orders
    
    set identity_insert tmp off
    

提交回复
热议问题