Monday, May 23, 2011

SQL Insert Queries

Single Insert using a Query:

The syntax for the INSERT statement is:

INSERT INTO table (column1, column2, ... columnn) VALUES (value1, value2, ... valuen);

Example #1 -

INSERT INTO USERS (id, user_name) VALUES (1001, 'GANESH');

Multiple Records insert using a single insert statement:

The above example is simple demo of the insert statement. I had a scenario in which I have to insert Multiple records by using a select sub query. I searched and found a way to insert multiple records using a single query.

INSERT INTO TABLE1 (column1, column2) select column21, column22 from Table2;

Example #2 -

INSERT INTO USERS (id, user_name) select id, userid from TEMP_USERS;