Thursday, July 18, 2019

sql - Insert into ... values ( SELECT ... FROM ... )



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 to remember the correct syntax for the SQL engine of the day (MySQL, Oracle, SQL Server, Informix, and DB2).



Is there a silver-bullet syntax coming from an SQL standard (for example, SQL-92) that would allow me to insert the values without worrying about the underlying database?



Answer



Try:



INSERT INTO table1 ( column1 )
SELECT col1
FROM table2


This is standard ANSI SQL and should work on any DBMS




It definitely works for:




  • Oracle

  • MS SQL Server

  • MySQL

  • Postgres

  • SQLite v3

  • Teradata

  • DB2


  • Sybase

  • Vertica

  • HSQLDB

  • H2

  • AWS RedShift

  • SAP HANA


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...