简体   繁体   中英

insert multiple rows in single SQL

I am trying to insert multiple rows with SQL statement.

For that i refered this question.

According to top scored answer in this qestion, i made following query:

INSERT INTO login
(LogInID,Password)
UNION ALL
SELECT 'Name1','pass1'
UNION ALL
SELECT 'Name2','pass2'

But when i try to execute this one, it gives me error:

Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'UNION'.

What can be the mistake in this query?

Is this a wrong approach?

Please help me.

NOTE: I am using SQL SERVER 2005

you have to remove UNION ALL before the first SELECT .

INSERT INTO login (LogInID,Password)
SELECT 'Name1','pass1'
UNION ALL
SELECT 'Name2','pass2'

Even though it does not provides an answer to your original question I think it's worth knowing that SQL Server provides another syntax using the VALUES syntax:

insert into login values
('Name1','pass1'), 
('Name2','pass2'),
('Name3','pass3')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM