本文轉自:
http://www.wangchao.net.cn/bbsdetail_44678.html
數據庫測試中,常常需要對數據庫中的表進行填充或者批量更改數據的操作,可以通過游標來實現對每一個查詢記錄的操作,通過rand()函數的使用獲得隨機數,將隨機數插入到表中,即可更新或填充數據表。
這里涉及到游標的使用,使用游標大體需要經過以下幾個步驟:
1.定義游標:declare cursor
2.打開游標:open cursor
3.取得游標中單個的記錄,并將記錄中的字段賦值給變量。fetch cursor
(每取一個值,游標會自動前移)
4.循環讀取游標,并對每一個記錄進行處理。fetch與fetch next 是等價的。
5.關閉并釋放游標,close cursor, deallocate cursor。
下面給出一個批量更改數據庫中記錄的例子,這個例子把價目表中所有料品的價格用0到100之間的數值更新,原價目表中所有料品的價格都為0,更新之后所有的價格都是0到100之間的隨機數:
use GuruERP
-- 定義游標MyTestCursor:
declare MyTestCursor cursor
for select PGI_ITM_CODE,PGI_ListPrice from TBLPRICELISTGROUPITEM
/*從表中選取兩個字段*/
/* 表TBLPRICELISTGROUPITEM中的字段PGI_ITM_CODE是Unique Key */
-- 打開游標MyTestCursor:
open MyTestCursor
declare @PGI_ITM_CODE char(28)
declare @PGI_ListPrice float
--fetch取出游標所指的記錄,并將記錄結果存入到變量中:
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 MyTestCursor
deallocate MyTestCursor
再重復一下,使用游標批量更改或填充數據庫,大體經過declare,open,fetch,loop fetch,close and deallocate 五個步驟。
備注1:
while循環體以BEGIN開始,以END結束,當條件為真時循環繼續,為假則結束
備注2:
@@FETCH_STATUS是sql server中的一個變量,下面是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 FOR
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
數據庫測試中,常常需要對數據庫中的表進行填充或者批量更改數據的操作,可以通過游標來實現對每一個查詢記錄的操作,通過rand()函數的使用獲得隨機數,將隨機數插入到表中,即可更新或填充數據表。
這里涉及到游標的使用,使用游標大體需要經過以下幾個步驟:
1.定義游標:declare cursor
2.打開游標:open cursor
3.取得游標中單個的記錄,并將記錄中的字段賦值給變量。fetch cursor
(每取一個值,游標會自動前移)
4.循環讀取游標,并對每一個記錄進行處理。fetch與fetch next 是等價的。
5.關閉并釋放游標,close cursor, deallocate cursor。
下面給出一個批量更改數據庫中記錄的例子,這個例子把價目表中所有料品的價格用0到100之間的數值更新,原價目表中所有料品的價格都為0,更新之后所有的價格都是0到100之間的隨機數:
use GuruERP
-- 定義游標MyTestCursor:
declare MyTestCursor cursor
for select PGI_ITM_CODE,PGI_ListPrice from TBLPRICELISTGROUPITEM
/*從表中選取兩個字段*/
/* 表TBLPRICELISTGROUPITEM中的字段PGI_ITM_CODE是Unique Key */
-- 打開游標MyTestCursor:
open MyTestCursor
declare @PGI_ITM_CODE char(28)
declare @PGI_ListPrice float
--fetch取出游標所指的記錄,并將記錄結果存入到變量中:
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 MyTestCursor
deallocate MyTestCursor
再重復一下,使用游標批量更改或填充數據庫,大體經過declare,open,fetch,loop fetch,close and deallocate 五個步驟。
備注1:
while循環體以BEGIN開始,以END結束,當條件為真時循環繼續,為假則結束
備注2:
@@FETCH_STATUS是sql server中的一個變量,下面是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 FOR
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
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
