惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
T
Threatpost
阮一峰的网络日志
阮一峰的网络日志
美团技术团队
F
Fortinet All Blogs
The GitHub Blog
The GitHub Blog
月光博客
月光博客
V
Visual Studio Blog
T
Tailwind CSS Blog
Stack Overflow Blog
Stack Overflow Blog
博客园 - 聂微东
Jina AI
Jina AI
J
Java Code Geeks
Martin Fowler
Martin Fowler
大猫的无限游戏
大猫的无限游戏
Recorded Future
Recorded Future
C
Check Point Blog
腾讯CDC
N
Netflix TechBlog - Medium
aimingoo的专栏
aimingoo的专栏
罗磊的独立博客
Hacker News: Ask HN
Hacker News: Ask HN
SecWiki News
SecWiki News
博客园 - Franky
Hacker News - Newest:
Hacker News - Newest: "LLM"
N
News | PayPal Newsroom
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Security @ Cisco Blogs
W
WeLiveSecurity
The Last Watchdog
The Last Watchdog
Cloudbric
Cloudbric
F
Full Disclosure
The Cloudflare Blog
Y
Y Combinator Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
MongoDB | Blog
MongoDB | Blog
S
Schneier on Security
Schneier on Security
Schneier on Security
Spread Privacy
Spread Privacy
L
LINUX DO - 热门话题
AI
AI
N
News and Events Feed by Topic
T
Tor Project blog
P
Palo Alto Networks Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More
G
Google Developers Blog

博客园 - wangbin

Provider 错误 '80004005' 未指定的错误 的最终解决方法 C#(WinForm)实现软件注册 设置字段默认值sql语句 .net WINFORM 界面怎么做凹凸效果的分割线?就是横线 winform屏蔽Alt+F4组合键以防止用户关闭对话框 SQL Server 2005 数据类型和.Net数据类型的对应关系 我改行了 PHP $_FILES详解 - wangbin - 博客园 php $GLOBALS 超全局变量的理解 [转载]php-数组操作foreach、each、reset、list .net 随机数 C#混淆 xenocode使用说明 [原创]一个简单的药店用的会员积分管理系统 [原创]xml序列化 [原创]c# as用法 [原创]闲来无事,写了个c#的数据库附加工具,现附上源代码 一个能够在线创建flash网页的站点 [原创]我的cms项目 简洁、标准的对联广告代码
游标Cursor 使用小例 (SQLServer)
wangbin · 2011-06-18 · via 博客园 - wangbin

1 表table:

表名:TblTest
column  type length
-------------------------------
iId  int 4
testvalue int 4
testDesc char 10

2 游标示例

declare @MyTestCursor Cursor
 
set @MyTestCursor= Cursor for select iId,testvalue from TblTest

open @MyTestCursor
declare @iTestId int
declare @iTestValue int

fetch @MyTestCursor into @iTestId,@iTestValue

while @@fetch_status=0
 begin
  update TblTest set testvalue=floor(100*rand()) where iId=@iTestId
  fetch next from @MyTestCursor into @iTestId,@iTestValue
 end

select @iTestId as iOutId,@iTestValue as iOutValue

close @MyTestCursor
deallocate @MyTestCursor


3 讲解说明

数据库测试中,常常需要对数据库中的表进行填充或者批量更改数据的操作,可以通过游标来实现对每一个查询记录的操作,通过rand()函数

的使用获得随机数,将随机数插入到表中,即可更新或填充数据表。

    这里涉及到游标的使用,使用游标大体需要经过以下几个步骤:
  1.定义游标:declare cursor
  2.打开游标:open cursor
  3.取得游标中单个的记录,并将记录中的字段赋值给变量。fetch cursor
   (每取一个值,游标会自动前移)
  4.循环读取游标,并对每一个记录进行处理。fetch与fetch next 是等价的。
  5.关闭 并 释放游标,close cursor, deallocate cursor。


下面给出一个批量更改数据库中记录的例子,这个例子把测试表中所有测试值的值用0到100之间的数值更新,原测试表中所有测试值的值都为0

,更新之后所有的测试值都是0到100之间的随机数:

 -- 定义游标MyTestCursor:
 declare @MyTestCursor Cursor
 
 set @MyTestCursor= Cursor for select iId,testvalue from TblTest

 /*从表中选取两个字段*/
 -- 打开游标MyTestCursor:
 open @MyTestCursor
 declare @iTestId int
 declare @iTestValue int

 --fetch取出游标所指的记录,并将记录结果存入到变量中:
 fetch from @MyTestCursor into @iTestId,@iTestValue
 /***************** begin of loop *******************************/
 while @@fetch_status=0
  begin
   update TblTest set testvalue=floor(100*rand()) where iId=@iTestId
   fetch next from @MyTestCursor into @iTestId,@iTestValue
  end
 /***************** end of loop *******************************/

 select @iTestId as iOutId,@iTestValue as iOutValue
 
 /***********关闭游标释放游标:***************/
 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

附:

.@@FETCH_STATUS属于任何游标的
----------------------------------------------------------------------
该文章转载自网络大本营:http://www.xrss.cn/Dev/DataBase/200791716603.Html

.@@FETCH_STATUS属于任何游标的,只要任何一个游标被提取了,这个提取成功与否的状态就会保存到@@FETCH_STATUS中.
嵌套游标的原理类似这样:
declare 外层游标
open 外层游标
fetch next ...提取外层游标行
while @@FETCH_STATUS = 0
begin
    declare 内层游标
    open 内层游标
    ftech next ...提取内层游标行
    while @@FETCH_STATUS = 0
    begin
          .....处理内层游标
          ftech next ....内层游标向下移动一行
    end
    close 内层游标
    deallocate 内层游标
    fetch next ....内层游标处理结束后,外层游标才继续向下移动一行
end
close 外层游标
deallocate 外层游标
也就是说,外层游标每移动一行,就要重复进行内层游标定义,打开,循环,关闭,释放等操作,然后才能再向下移动行.