UseTempdb Go --Create test table Create TableTB(IDChar(8)) --Create a unique index used to automatically filter duplicate values Create Unique IndexIx_tbOnTB(ID) WithIgnore_dup_key Go --Test data insertion Processing Time,Record Start Time Declare@ DtDatetime Set@ Dt= Getdate() --Insert random data Set Nocount On Declare@ RowInt Set@ Row=1000000--Set the total number of records While@ Row>0 Begin -- display prompt information , indicates that multiple rows of data need to be inserted. Raiserror('Need % d rows',10,1,@ Row) WithNowait --Insert random bit encoding data Set Rowcount@ Row insert TB select id = right ( 100000000 + convert ( bigint , ABS ( checksum ( newid ()))), 8 ) FromSyscolumns C1,Syscolumns C2 Set@ Row=@ Row- @ Rowcount End --Display the time when data is inserted select begindate = @ dt , enddate = getdate (), Second= Datediff(Second,@ Dt, Getdate()), Go --Check whether the final result record is correct Select Count(*) FromTB Go --Delete Test Drop TableTB Declare @ DT datetime Set @ dt = getdate () Set nocount on -- the number of affected rows is not returned, which improves efficiency. Declare @ row int Set @ ROW = 1000000 While @ row> 0 Begin Raiserror ('need % d rows ', 10, 1, @ row) with Nowait -- output information to the client when duplicate rows occur. Set rowcount @ row -- Select... from syscolumns C1, the number of rows in syscolumns C2 will exceed 100 W. Here, we only take 100 W. Insert testtb (CODE) Select --- insert records at a time. If there are duplicate records, insert the record for the second time, and so on until records are inserted. Id = 'abcd' + right (100000000 + convert (bigint, ABS (checksum (newid (), 8) -- 100000000 is used to ensure that the generated random number has 8-bit length, From syscolumns C1, syscolumns C2 -- Here syscolumns is used only to obtain records larger. It does not matter if the random source is a table. Set @ ROW = @ row-@ rowcount -- set the number of records to be inserted during the Loop Based on the number of affected rows. The number of records is equal to the number of repeated records in this operation. End Select begindate = @ DT, enddate = getdate (), second = datediff (second, @ DT, getdate ()) Go Select count (*) from testtb Go /* My problem is (1) Why use checksum () to generate random numbers? (2) newid is a unique identifier. Since newid is unique, how can it be repeated? (3) http://msdn2.microsoft.com/zh-cn/library/ms189788.aspx Returns the verification value calculated based on a row or a group of expressions in the table. Checksum is used to generate a hash index. Is the hash table index numeric? */
Raiserror ('need % d rows ', 10, 1, @ row) with Nowait -- output information to the client when duplicate rows occur. -- This is a prompt message directly. If there is a duplicate message, the prompt message is automatically thrown by SQL. Insert testtb (CODE) Select --- insert records at a time. If there are duplicate records, insert the record for the second time, and so on until records are inserted. -- Duplicate data is automatically filtered out when duplicate data exists. Checksum Returns the checksum value calculated on the table row or expression list. Checksum is used to generate a hash index. Syntax Checksum (* | expression [,... n]) Parameters * Specify all columns in the table for calculation. If any column is of a non-comparable data type, checksum returns an error. The incomparable data types are text, ntext, image, cursor, and SQL _variant whose basic types are one of the first four data types. Expression Is an expression of any type except for the comparable data type. Return type Int 000
|