使用游标批量更新/填充数据库(The Using of Cursor)
2004-09-28 15:18
579 查看
Author:David Euler
Date: 2004/09/28
Email:de_euler-david@yahoo.com.cn
有任何问题,请与我联系:)
数据库测试中,常常需要对数据库中的表进行填充或者批量更改数据的操作,可以通过游标来实现对每一个查询记录的操作,通过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.
Examples
This example uses @@FETCH_STATUS to control cursor activities in a WHILE loop.
Date: 2004/09/28
Email:de_euler-david@yahoo.com.cn
有任何问题,请与我联系:)
数据库测试中,常常需要对数据库中的表进行填充或者批量更改数据的操作,可以通过游标来实现对每一个查询记录的操作,通过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. |
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
相关文章推荐
- 使用游标批量更新/填充数据库(The Using of Cursor)
- 【转】使用游标批量更改/填充数据表中的记录值(The Using of Cursor)
- 使用游标批量更改/填充数据表中的记录值(TheUsingofCursor)
- Oracle Procedure中使用游标cursor更新多表
- Mysql创建存储过程,使用游标Cursor循环更新
- Mysql游标使用 批量插入或更新数据
- 使用Go对数据库批量更新
- oracle使用dblink和cursor更新不同数据库的记录
- 【数据库SQL】批量更新促发器游标的方法
- 使用MagicAJax的AjaxPanel时有时会弹出"Using the AjaxCallHelper write methods outside of an AjaxCall is not allowed."
- #713 – 在拖拽操作中使用控件预览图作为鼠标(Setting the Cursor to an Image of an UIElement While Dragging)
- 使用DataAdpater自动批量更新DataSet中的数据到数据库
- net core生成数据库,使用Scaffold-DbContext报错“‘Scaffold-DbContext’ is not recognized as the name of a cmdlet”
- 数据库使用游标(Cursor)的问题
- lightinthebox火车头数据采集器(20120812更新) 使用火车头接口技术一键批量采集 批量发布 批量去水印 接口技术
- Mysql创建存储过程,使用游标Cursor循环更新
- The NOTE of learning ASP.NET [19] 关于GC(内存回收机制)、对象的销毁和using的使用
- 使用游标批量更新数据
- oracle pl/sql之使用游标变量更新数据库
- 使用DataAdpater自动批量更新DataSet中的数据到数据库