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