These days of practice. Come across a little bit of a problem. For example, I have scores initialized in the score table for all 0. So now I want to randomly generate 100 or less data to write in, how should I do it? You can use a few small functions.
One is rand ()
It is responsible for generating random numbers.
One is cast ()
It is responsible for type conversions.
One is round ()
It is responsible for rounding the values. In addition to this, floor () represents the downward value. and ceiling () represents the upward value.
Next, it is the operation of the table score. The first step is to generate a temporary table #temp.
Select Identity (int,1,1) ID, * to temp from score
Note that the ID inside is the column I added myself, assuming you have a primary key in your table. And not a combined primary key. Can not add ID to this column.
In fact. The central idea of a batch of data that changes a table is. Let's remember that the table has self-growing columns, assuming the original is the best, if not, create a temporary table. Let the temporary table have such a column. Then make two tables connected. After the connection, each item has a self-growing column, then a temporary variable is set, which is compared with the self-growing column and then the value of the pro-edge variable. Changes the table's data row by line.
That's not clear, just add a piece of code.
declare @total int, @cur int Select @total =count (*) from #tempset @tot =1while @tot <[email protected] BeginUpdate score Set chengji= cast (100*rand () as int) from score T1, #temp t2where t1.stu=t2.stuand T1.course=t2.courseand [Email protected]set @[email protected]+1end
People understand the code should be able to comprehend what I mean.
"2015/08/06" How to bulk change data for a table in SQL Server