Use the cursor to batch change/fill the record value in the data table (the using of cursor)
Source: Internet
Author: User
This article from: http://www.wangchao.net.cn/bbsdetail_44678.html
In database testing, you often need to fill tables in the database or modify data in batches. You can use a cursor to perform operations on each query record, through RAND () the function obtains a random number and inserts the random number into the table to update or fill the data table.
To use a cursor, follow these steps:
1. Define the cursor: declare cursor
2. Open cursor: Open cursor
3. Obtain a single record in the cursor and assign the fields in the record to the variable. Fetch cursor
(Each time a value is obtained, the cursor will automatically move forward)
4. Read the cursor cyclically and process each record. Fetch is equivalent to fetch next.
5. Close and release the cursor, close cursor, and deallocate cursor.
The following is an example of batch change records in the database. In this example, the price of all the materials in the price list is updated with a value ranging from 0 to 100. The price of all the materials in the original price list is 0, after the update, all prices are random numbers between 0 and 100:
Use guruerp
-- Define the cursor mytestcursor:
Declare mytestcursor cursor
For select pgi_itm_code, pgi_listprice from tblpricelistgroupitem
/* Select two fields from the table */
/* The pgi_itm_code field in the tblpricelistgroupitem table is the unique key */
-- Open the cursor mytestcursor:
Open mytestcursor
Declare @ pgi_itm_code char (28)
Declare @ pgi_listprice float
-- Fetch retrieves the record referred to by the cursor and saves the record result to the variable:
Fetch from mytestcursor into @ pgi_itm_code, @ pgi_listprice
/***************** Begin of loop ******************* ************/
While @ fetch_status = 0
Begin
Update tblpricelistgroupitem set pgi_listprice = floor (100 * rand () where pgi_itm_code = @ pgi_itm_code
Fetch next from mytestcursor into @ pgi_itm_code, @ pgi_listprice
End
/***************** End of loop ******************* ************/
Select @ pgi_itm_code as code, @ pgi_listprice as price
/*********** Close the cursor and release the cursor :***************/
Close mytestcursor
Deallocate mytestcursor
Repeat it and use the cursor to modify or fill the database in batches. The steps include declare, open, fetch, loop fetch, close and deallocate.
Note 1:
The while loop begins with begin and ends with end. When the condition is true, the loop continues. If the condition is false, the loop ends.
NOTE 2:
@ Fetch_status is a variable in SQL Server. The following is an explanation on SQL Server books online:
Returns the status of the last cursor fetch statement issued against any cursor currently opened by the connection.
Return Value
Description
0
Fetch statement was successful.
-1
Fetch statement failed or the row was beyond the result set.
-2
Row fetched is missing.
Examples
This example uses @ fetch_status to control cursor activities in a while loop.
Declare employee_cursor cursor
Select lastname, firstname from northwind. DBO. Employees
Open employee_cursor
Fetch next from employee_cursor
While @ fetch_status = 0
Begin
Fetch next from employee_cursor
End
Close employee_cursor
Deallocate employee_cursor
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