SQL inserts multiple records in batches

Source: Internet
Author: User

To facilitate testing, we usually insert many records in the database for testing. However, manual addition is troublesome, so batch addition is generally used.

Note:

1) The example table structure is as follows:

Column name Data Type
ID Uniqueidentifier
Userno Nvarchar (50)
Password Nvarchar (255)
Lastname Nvarchar (255)
Firstname Nvarchar (255)

2) belowCodeTested in MS sql2005.

1. Insert multiple records manually written at one time.

For example, the following code inserts two records at a time.

Insert   Into Pw_user (ID, userno, password, firstname, lastname)
Select   Newid (), ' Test00001 ' , ' Test00001 ' , ' Zhang ' , ' I '
Union   All  
Select   Newid (), ' Test00002 ' , ' Test00002 ' , ' Zhang ' , ' II '

2. Multiple records generated by one insert statement.

For example, the following code inserts 100 records at a time.

Code Declare   @ I   Int , @ Value   Nvarchar
Begin
Set   @ I = 1 -- Initialization
While   @ I <= 100
Begin
Insert   Into Pw_user (ID, userno, password, firstname, lastname)
Values ( Newid (), ' Test0000 ' + Cast ( @ I   As   Nvarchar ), ' Test0000 ' + Cast ( @ I   As   Nvarchar ), ' Zhang ' , ' 0000 ' + Cast ( @ I   As   Nvarchar ) )
Set   @ I = @ I + 1
End
End

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.