SQL Server inserts multi-line data statements into a table in bulk

Source: Internet
Author: User
Tags bulk insert

You need two tables with a large number of non-repeating rows for your own test, and the more rows you have, the better. Manually write SQL statements that insert data into a table in batches by looping, taking into account the avoidance of a consistent problem and setting the parity line differently. personal level is limited, if there is mistake, also look correct.

The statements are as follows:

1 --BULK Insert a large number of data statements into a table (odd and even different)2 3 --determine if the test table exists, then delete and then create4 if exists(Select 1  fromsysobjectswhereXtype='u'  andName='table_test' )5 Drop Tabletable_test6 Create TableTable_test (7Idint,8NameChar( -),9AddressChar( -),TenGenderChar(Ten) One ) A  - Declare @n int - Declare @name  varchar( -)--Using Variables the Declare @name1 varchar( -)--set odd for - Declare @name2 varchar( -)--Set even numbers - Declare @addre varchar( -)--Using Variables - Declare @sql varchar( $) + Set @n=0 - Set @addre=''s Home' + Set @name=' ' A Set @name1='Tom' at Set @name2='Jarry' -  while @n<998--do not 2999, nor 1999, as long as 998, yes, as long as 998 - begin -     Set @n=@n+1 -     if @n%2=1--Odd Rows -     begin in         Set @name=@name1+Convert(varchar,@n)--combination of name and number to avoid duplicate Name column -         Set @addre=@name+@addre--address by the name of the "home" combination to avoid different to         Set @sql='INSERT INTO Table_test (Id,name,address,gender) VALUES ('+Convert(int,@n)+','+@name+','+@addre+',"'male"')' +         Set @addre=''s Home'--because @address has already assigned a new value, manually restore the initial value, avoid duplicate assignment cause the word descriptor long -         Print(@sql) the         exec(@sql) *     End $     Else--even rowsPanax Notoginseng     begin -         Set @name=@name2+Convert(varchar,@n) the         Set @addre=@name+@addre +         Set @sql='INSERT INTO Table_test (Id,name,address,gender) VALUES ('+Convert(int,@n)+','+@name+','+@addre+',"'female"')' A         Set @addre=''s Home' the         Print(@sql) +         exec(@sql) -     End $ End

This allows you to bulk insert multiple rows into the table as required.

I tried inserting 18750000 rows myself.

Nice

SQL Server inserts multi-line data statements into a table in bulk

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.