SQL Server: insert multiple records using one statement Block

Source: Internet
Author: User

In our daily operations, we have to insert multiple records to a table in the database at a time, but the first thought is to copy and paste n multiple repeated insert into statements, in case an error occurs when a semicolon or multiple Commas are missing, it will be a waste of time and work.
In addition to the method mentioned above, there are actually two methods, which are simpler than the previous one.
The first is the previous method:

Insert into mytable (ID, name) values (1 ,'123'); Insert into mytable (ID, name) values (2 ,'456'); Insert into mytable (ID, name) values (3 ,'789');

The second method is to use Union all for insert operations:

Insert into mytable (ID, name) Select 4 ,'000'Union allselect 5 ,'001'Union allselect 6 ,'002'

Is it easier than the first method? It is said to be faster than the first one!

The third method is a bit special. It is exclusive to SQL Server. Therefore, if you are not SQL Server, you cannot use this method. So please upgrade it! Experience the benefits of SQL server2008.

Insert into mytable (ID, name) values (7 ,'003'), (8 ,'004'), (9 ,'005')

 

-- EOF --

Author: xingbaifang

Web: http://xbf321.cnblogs.com/

Time: 2010.3.14

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.