"2015/08/06" How to bulk change data for a table in SQL Server

Source: Internet
Author: User
Tags rand

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

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.