使用游标批量更改/填充数据表中的记录值(TheUsingofCursor)
来源:互联网 发布:二手西门子编程电缆 编辑:程序博客网 时间:2024/05/16 15:23
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
Author:DavidEuler
Date:2004/09/28
Email:de_euler-david@yahoo.com.cn
有任何问题,请与我联系:)
测试中,常常需要对数据库中的表进行填充或者批量更改数据的操作,可以通过游标来实现对每一个查询记录的操作,通过rand()函数的使用获得随机数,将随机数插入到表中,即可更新或填充数据表。
这里涉及到游标的使用,使用游标大体需要经过以下几个步骤:
1.定义游标:declarecursor
2.打开游标:opencursor
3.取得游标中单个的记录,并将记录中的字段赋值给变量。fetchcursor
(每取一个值,游标会自动前移)
4.循环读取游标,并对每一个记录进行处理。fetch与fetchnext是等价的。
5.关闭并释放游标,closecursor,deallocatecursor。
下面给出一个批量更改数据库中记录的例子,这个例子把价目表中所有料品的价格用0到100之间的数值更新,原价目表中所有料品的价格都为0,更新之后所有的价格都是0到100之间的随机数:
useGuruERP
--定义游标MyTestCursor:
declare MyTestCursorcursor
forselectPGI_ITM_CODE,PGI_ListPricefromTBLPRICELISTGROUPITEM
/*从表中选取两个字段*/
/*表TBLPRICELISTGROUPITEM中的字段PGI_ITM_CODE是UniqueKey */
--打开游标MyTestCursor:
openMyTestCursor
declare@PGI_ITM_CODEchar(28)
declare@PGI_ListPricefloat
--fetch取出游标所指的记录,并将记录结果存入到变量中:
fetchfromMyTestCursorinto@PGI_ITM_CODE,@PGI_ListPrice
/***************** beginofloop*******************************/
while@@FETCH_STATUS=0
Begin
updateTBLPRICELISTGROUPITEMsetPGI_ListPrice=floor(100*rand())wherePGI_ITM_CODE=@PGI_ITM_CODE
fetchnextfromMyTestCursorinto@PGI_ITM_CODE,@PGI_ListPrice
End
/***************** endofloop*******************************/
select @PGI_ITM_CODEascode,@PGI_ListPriceasprice
/***********关闭游标,释放游标:***************/
closeMyTestCursor
deallocateMyTestCursor
再重复一下,使用游标批量更改或填充数据库,大体经过declare,open,fetch,loopfetch,closeanddeallocate五个步骤。
备注1:
while循环体以BEGIN开始,以END结束,当条件为真时循环继续,为假则结束
备注2:
@@FETCH_STATUS是sqlserver中的一个变量,下面是SQL server Booksonline上的解释:
ReturnsthestatusofthelastcursorFETCHstatementissuedagainstanycursorcurrentlyopenedbytheconnection.
ReturnvalueDescription0FETCHstatementwassuccessful.-1FETCHstatementfailedortherowwasbeyondtheresultset.-2Rowfetchedismissing.
Examples
Thisexampleuses@@FETCH_STATUStocontrolcursoractivitiesinaWHILEloop.
DECLAREEmployee_CursorCURSORFORSELECTLastName,FirstNameFROMNorthwind.dbo.EmployeesOPENEmployee_CursorFETCHNEXTFROMEmployee_CursorWHILE@@FETCH_STATUS=0BEGINFETCHNEXTFROMEmployee_CursorENDCLOSEEmployee_CursorDEALLOCATEEmployee_Cursor
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
Author:DavidEuler
Date:2004/09/28
Email:de_euler-david@yahoo.com.cn
有任何问题,请与我联系:)
测试中,常常需要对数据库中的表进行填充或者批量更改数据的操作,可以通过游标来实现对每一个查询记录的操作,通过rand()函数的使用获得随机数,将随机数插入到表中,即可更新或填充数据表。
这里涉及到游标的使用,使用游标大体需要经过以下几个步骤:
1.定义游标:declarecursor
2.打开游标:opencursor
3.取得游标中单个的记录,并将记录中的字段赋值给变量。fetchcursor
(每取一个值,游标会自动前移)
4.循环读取游标,并对每一个记录进行处理。fetch与fetchnext是等价的。
5.关闭并释放游标,closecursor,deallocatecursor。
下面给出一个批量更改数据库中记录的例子,这个例子把价目表中所有料品的价格用0到100之间的数值更新,原价目表中所有料品的价格都为0,更新之后所有的价格都是0到100之间的随机数:
useGuruERP
--定义游标MyTestCursor:
declare MyTestCursorcursor
forselectPGI_ITM_CODE,PGI_ListPricefromTBLPRICELISTGROUPITEM
/*从表中选取两个字段*/
/*表TBLPRICELISTGROUPITEM中的字段PGI_ITM_CODE是UniqueKey */
--打开游标MyTestCursor:
openMyTestCursor
declare@PGI_ITM_CODEchar(28)
declare@PGI_ListPricefloat
--fetch取出游标所指的记录,并将记录结果存入到变量中:
fetchfromMyTestCursorinto@PGI_ITM_CODE,@PGI_ListPrice
/***************** beginofloop*******************************/
while@@FETCH_STATUS=0
Begin
updateTBLPRICELISTGROUPITEMsetPGI_ListPrice=floor(100*rand())wherePGI_ITM_CODE=@PGI_ITM_CODE
fetchnextfromMyTestCursorinto@PGI_ITM_CODE,@PGI_ListPrice
End
/***************** endofloop*******************************/
select @PGI_ITM_CODEascode,@PGI_ListPriceasprice
/***********关闭游标,释放游标:***************/
closeMyTestCursor
deallocateMyTestCursor
再重复一下,使用游标批量更改或填充数据库,大体经过declare,open,fetch,loopfetch,closeanddeallocate五个步骤。
备注1:
while循环体以BEGIN开始,以END结束,当条件为真时循环继续,为假则结束
备注2:
@@FETCH_STATUS是sqlserver中的一个变量,下面是SQL server Booksonline上的解释:
ReturnsthestatusofthelastcursorFETCHstatementissuedagainstanycursorcurrentlyopenedbytheconnection.
ReturnvalueDescription0FETCHstatementwassuccessful.-1FETCHstatementfailedortherowwasbeyondtheresultset.-2Rowfetchedismissing.
Examples
Thisexampleuses@@FETCH_STATUStocontrolcursoractivitiesinaWHILEloop.
DECLAREEmployee_CursorCURSORFORSELECTLastName,FirstNameFROMNorthwind.dbo.EmployeesOPENEmployee_CursorFETCHNEXTFROMEmployee_CursorWHILE@@FETCH_STATUS=0BEGINFETCHNEXTFROMEmployee_CursorENDCLOSEEmployee_CursorDEALLOCATEEmployee_Cursor
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
- 使用游标批量更改/填充数据表中的记录值(TheUsingofCursor)
- Oracle使用游标删除所有用户数据表中的所有记录脚本
- 使用游标批量更新/填充数据库(The Using of Cursor)
- 使用游标批量更新/填充数据库(The Using of Cursor)
- 操作数据表中的记录
- 批量处理数据表中的空格
- 使用全外连接比较两个数据表中的不同记录
- PHP中批量更新数据表中多条记录
- android使用ant自动打包(更改java文件中的属性值,批量自动生成)
- 使用游标批量更新数据
- 使用游标修改数据表的方法SQL2005
- Oracle使用游标查询所有数据表备注
- 删除数据表中的重复记录
- 删除数据表中的重复记录
- MYSQL操作数据表中的记录
- Oracle 游标记录使用示例
- 记录mysql 游标使用示例
- oracle 使用游标批量分段插入数据
- 利用Winsock下载文件(支持断点续传)
- 物料编码的原则
- 字符集问题的初步探讨(五)
- 初学触发器
- 为什么AUTOTRACE不会在执行计划中显示分区截断(partitionpruning)?
- 使用游标批量更改/填充数据表中的记录值(TheUsingofCursor)
- 将表的记录个数存入到变量中
- 远程管理sqlserver的注册方法
- --排列名次的sqlserver函数-------------(原创)
- 数据备份与数据容灾
- IMP-00020错误的解决方法
- [SQlServer学习]stepbystep前言!
- 对参数optimizer_index_cost_adj的一点理解_041009
- 面向实时物流作业的板坯库管理系统