Monday, 11 April 2016

SQL - Insert Multiple Rows in a Single Query

Below code snippets can be used to insert multiple rows in a Single SQL Query Statement.
This will be faster than using multiple Insert statements.

INSERT INTO MyTable ( Column1, Column2 ) 
VALUES
  ( Value1, Value2 ), 
  ( Value1, Value2 );

-- Ex:

INSERT INTO tblPerson (First,Last) 
VALUES 
  ('Fred','Smith'),
  ('John','Smith'),
  ('Michael','Smith'),
  ('Robert','Smith');

-- If data is from another table
INSERT INTO table1 ( col1, col2, col3, col4 )
SELECT  table2.column1, table2.column2, 8, 'some string etc.'
FROM    table2
WHERE   table2.ID = 7;