SQL SERVER Bulk Build number

Source: Internet
Author: User
Tags rtrim

Begin:

In testing, in order to simulate orders, there is a requirement for the DBA, how to quickly batch generate orders through the background database script.

Analysis

Standing in the database perspective, bulk generate orders, which is the bulk of the row data in the table.

In SQL, two table (e.g. A, B) can be combined with a cross join to form a Cartesian product, 1

  

Figure 1

If, for the combined result of Figure 1, a combination of iterations, then you can get a result of 16 rows, 2:

  

Figure 2

In SQL Sever, the following SQL statement analyzes the need for a, B table combination, how many times an iteration can generate records for millions of rows,

  

 UsetempdbGoDeclare @x bigint =2Declare @i int=1 while(1=1)begin        Set @x=Square(@x)    if @ @ERROR<>0  Break; Print RTrim(@i)+' : '+RTrim(sqrt(@x))+'x'+RTrim(sqrt(@x))+' = '+RTrim(@x); Set @i+=1;End

From this you can see a A-B table combination, which requires iterating 5 times to generate millions of rows of data.

Realize:

SQL SERVER Code:

; withA0 as(SelectId=1 Union  All SelectId=1), A1 as(Selecta.ID fromA0 a,a0 b), A2 as(Selecta.ID fromA1 a,a1 B), A3 as(Selecta.ID fromA2 a,a2 b), A4 as(Selecta.ID fromA3 a,a3 B), T as(SelectId=Row_number () Over(Order  bya.ID) fromA4 a,a4 b)Select Top 1000000Id fromT

Here, it is possible to generate data rows in batches. Additional conditions or data can be attached to meet the needs of the beginning, depending on the actual needs.

SQL SERVER Bulk Build number

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.