Inserting multiple rows in a single SQL query? [duplicate] - sql

This question already has answers here:
Insert multiple rows WITHOUT repeating the "INSERT INTO ..." part of the statement?
(17 answers)
Closed 9 years ago.
I have multiple set of data to insert at once, say 4 rows. My table has three columns: Person, Id and Office.
INSERT INTO MyTable VALUES ("John", 123, "Lloyds Office");
INSERT INTO MyTable VALUES ("Jane", 124, "Lloyds Office");
INSERT INTO MyTable VALUES ("Billy", 125, "London Office");
INSERT INTO MyTable VALUES ("Miranda", 126, "Bristol Office");
Can I insert all 4 rows in a single SQL statement?

In SQL Server 2008 you can insert multiple rows using a single SQL INSERT statement.
INSERT INTO MyTable ( Column1, Column2 ) VALUES
( Value1, Value2 ), ( Value1, Value2 )
For reference to this have a look at MOC Course 2778A - Writing SQL Queries in SQL Server 2008.
For example:
INSERT INTO MyTable
( Column1, Column2, Column3 )
VALUES
('John', 123, 'Lloyds Office'),
('Jane', 124, 'Lloyds Office'),
('Billy', 125, 'London Office'),
('Miranda', 126, 'Bristol Office');

If you are inserting into a single table, you can write your query like this (maybe only in MySQL):
INSERT INTO table1 (First, Last)
VALUES
('Fred', 'Smith'),
('John', 'Smith'),
('Michael', 'Smith'),
('Robert', 'Smith');

NOTE: This answer is for SQL Server 2005. For SQL Server 2008 and later, there are much better methods as seen in the other answers.
You can use INSERT with SELECT UNION ALL:
INSERT INTO MyTable (FirstCol, SecondCol)
SELECT 'First' ,1
UNION ALL
SELECT 'Second' ,2
UNION ALL
SELECT 'Third' ,3
...
Only for small datasets though, which should be fine for your 4 records.

INSERT statements that use VALUES syntax can insert multiple rows. To do this, include multiple lists of column values, each enclosed within parentheses and separated by commas.
Example:
INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);

Related

Insert multiple records in Oracle Database

Example:
INSERT INTO TABLE ( USERID, USERNAME) VALUES (1,"ok1"),(2,"ok2");
As i understood ,Multiple rows insertion not allowed in Oracle Database.
Please confirm me if any other alternatives for inserting multiple records into oracle DB with above format.
Oracle only allows one row to be inserted at a time. So use two inserts:
INSERT INTO TABLE ( USERID, USERNAME)
VALUES (1, 'ok1');
INSERT INTO TABLE ( USERID, USERNAME)
VALUES (2, 'ok2');
Or use INSERT . . . SELECT:
INSERT INTO TABLE ( USERID, USERNAME)
SELECT 1 'ok1' FROM DUAL UNION ALL
SELECT 2, 'ok2' FROM DUAL;

Inserting data into Oracle table (SQL)

I already have a table built in oracle.
Im trying to insert some data like this:
INSERT INTO movies_actor('name','id')
VALUES ('Nuno','2'), ('Pedro','3'), ('Jose','1');
select * from movies_actor;
I always get this error
ORA-00928: missing SELECT keyword
What am I doing wrong?
I don't think you need the single quote around your field names.
You need to do:
INSERT INTO TableName(Column1, Column2)
VALUES('Nuno', '2');
In your example, it would be:
INSERT INTO movies_actor(name, id)
VALUES ('Nuno','2');
INSERT INTO movies_actor(name, id)
VALUES ('Pedro','3');
INSERT INTO movies_actor(name, id)
VALUES ('Jose','1');
select * from movies_actor;
Another way.
insert into table
(field1, field2)
select value1, value2
from dual
union
select value3, value4
from dual
etc
You cannot insert multiple records in one statement using VALUES. You can either use Tenzin's solution or use INSERT ALL :
INSERT ALL
INTO movies_actor(name, id) VALUES ('Nuno', '2')
INTO movies_actor(name, id) VALUES ('Pedro', '3')
INTO movies_actor(name, id) VALUES ('Jose', '1')
SELECT * FROM dual;

Insert multiple rows of data in a single SQL statement [duplicate]

This question already has answers here:
Inserting multiple rows in a single SQL query? [duplicate]
(4 answers)
Closed 7 years ago.
I have multiple set of data to insert at once
INSERT INTO MyTable VALUES ("John", "Doe", 1234567890, "employee", "");
INSERT INTO MyTable VALUES ("Susen", "Gupta", 1234567890, "leander");
INSERT INTO MyTable VALUES ("Karn", "Share", 1234567890, "employee", "home");
I want to insert multiple rows in a single SQL statement. And can it possible to do it with different number of values.
Multi-row insert has been part of the SQL standard since SQL-92, and many of the modern DBMS' support it. That would allow you to do something like:
insert into MyTable ( Name, Id, Location)
values ('John', 123, 'Lloyds Office'),
('Jane', 124, 'Lloyds Office'),
('Billy', 125, 'London Office'),
('Miranda', 126, 'Bristol Office');
You'll notice I'm using the full form of insert into there, listing the columns to use. I prefer that since it makes you immune from whatever order the columns default to.
If your particular DBMS does not support it, you could do it as part of a transaction which depends on the DBMS but basically looks like:
begin transaction;
insert into MyTable (Name,Id,Location) values ('John',123,'Lloyds Office');
insert into MyTable (Name,Id,Location) values ('Jane',124,'Lloyds Office'),
insert into MyTable (Name,Id,Location) values ('Billy',125,'London Office'),
insert into MyTable (Name,Id,Location) values ('Miranda',126,'Bristol Office');
commit transaction;
This makes the operation atomic, either inserting all values or inserting none.
Yes you can, but it depends on the SQL taste that you are using :) , for example in mysql, and sqlserver:
INSERT INTO Table ( col1, col2 ) VALUES
( val1_1, val1_2 ), ( val2_1, val2_2 ), ( val3_1, val3_2 );
But in oracle:
INSERT ALL
INTO Table (col1, col2, col3) VALUES ('val1_1', 'val1_2', 'val1_3')
INTO Table (col1, col2, col3) VALUES ('val2_1', 'val2_2', 'val2_3')
INTO Table (col1, col2, col3) VALUES ('val3_1', 'val3_2', 'val3_3')
.
.
.
SELECT 1 FROM DUAL;
In SQL Server, you can do this:
INSERT INTO MyTable VALUES ("John", 123, "Lloyds Office"),
("Jane", 124, "Lloyds Office"),
("Billy", 125, "London Office"),
("Miranda", 126, "Bristol Office")

bulky insert record to database

I need to insert approximatlly 10 records.I am thinking foreach loop,do u think is it the best way to do that.whenn searching what is the best method to do that,I find something like this,
GO
INSERT INTO MyTable (FirstCol, SecondCol)
SELECT 'First' ,1
UNION ALL
SELECT 'Second' ,2
UNION ALL
SELECT 'Third' ,3
UNION ALL
SELECT 'Fourth' ,4
UNION ALL
SELECT 'Fifth' ,5
GO
what is the differences between the this and foreach loop.which one is the better.
thank you all
INSERT INTO MyTable (FirstCol, SecondCol)
VALUES ('First',1),
('Second',2),
('Third',3),
('Fourth',4),
('Fifth',5)
GO
Both the foreach loop and the union all methods would be what is called "Row by Agonizing Row" approach
If you have MSSQL 2008 or later, you can use multi-valued insert:
INSERT INTO mytable (id, name)
VALUES (1, 'One'),
(2, 'Two'),
(3, 'Three')
Unfortunately, this syntax is not supported by MSSQL 2005 and earlier.
Just Create a single for inserting all the record..
Because foreach loop will hit the database 10 times and u will take more time and resources..
So better to make single query for all the 10..
INSERT INTO Table ( FirstCol, SecondCol) VALUES
( Value1, Value2 ), ( Value1, Value2 )...

Insert multiple values using INSERT INTO (SQL Server 2005)

In SQL Server 2005, I'm trying to figure out why I'm not able to insert multiple fields into a table. The following query, which inserts one record, works fine:
INSERT INTO [MyDB].[dbo].[MyTable]
([FieldID]
,[Description])
VALUES
(1000,N'test')
However, the following query, which specifies more than one value, fails:
INSERT INTO [MyDB].[dbo].[MyTable]
([FieldID]
,[Description])
VALUES
(1000,N'test'),(1001,N'test2')
I get this message:
Msg 102, Level 15, State 1, Line 5
Incorrect syntax near ','.
When I looked up the help for INSERT in SQL Sever Management Studio, one of their examples showed using the "Values" syntax that I used (with groups of values in parentheses and separated by commas). The help documentation I found in SQL Server Management Studio looks like it's for SQL Server 2008, so perhaps that's the reason that the insert doesn't work. Either way, I can't figure out why it won't work.
The syntax you are using is new to SQL Server 2008:
INSERT INTO [MyDB].[dbo].[MyTable]
([FieldID]
,[Description])
VALUES
(1000,N'test'),(1001,N'test2')
For SQL Server 2005, you will have to use multiple INSERT statements:
INSERT INTO [MyDB].[dbo].[MyTable]
([FieldID]
,[Description])
VALUES
(1000,N'test')
INSERT INTO [MyDB].[dbo].[MyTable]
([FieldID]
,[Description])
VALUES
(1001,N'test2')
One other option is to use UNION ALL:
INSERT INTO [MyDB].[dbo].[MyTable]
([FieldID]
,[Description])
SELECT 1000, N'test' UNION ALL
SELECT 1001, N'test2'
You can also use the following syntax:-
INSERT INTO MyTable (FirstCol, SecondCol)
SELECT 'First' ,1
UNION ALL
SELECT 'Second' ,2
UNION ALL
SELECT 'Third' ,3
UNION ALL
SELECT 'Fourth' ,4
UNION ALL
SELECT 'Fifth' ,5
GO
From here
In SQL Server 2008,2012,2014 you can insert multiple rows using a single SQL INSERT statement.
INSERT INTO TableName ( Column1, Column2 ) VALUES
( Value1, Value2 ), ( Value1, Value2 )
Another way
INSERT INTO TableName (Column1, Column2 )
SELECT Value1 ,Value2
UNION ALL
SELECT Value1 ,Value2
UNION ALL
SELECT Value1 ,Value2
UNION ALL
SELECT Value1 ,Value2
UNION ALL
SELECT Value1 ,Value2