首页 新闻 赞助 找找看

游标读数据问题.

0
悬赏园豆:10 [已关闭问题] 关闭于 2008-04-24 17:57
<P>我定义一个字符串,想要用游标把他依次读出来,代码如下,不过结果不对,请教我写的代码错那里了?&nbsp; 我想要的结果是:<BR>1<BR>1<BR>2<BR>2</P> <P>declare @b varchar(20),@name varchar(20)<BR>&nbsp;set @b = ''<BR>&nbsp;set @name = '1,1,2,2'<BR>&nbsp;declare mycursor cursor <BR>&nbsp;for <BR>&nbsp;&nbsp; select @name<BR>&nbsp;&nbsp; open mycursor<BR>&nbsp;&nbsp; fetch mycursor into @name<BR>&nbsp;&nbsp; while @@fetch_status&lt;&gt;-1<BR>&nbsp;&nbsp;if @@fetch_status &lt;&gt;-2<BR>&nbsp;&nbsp;&nbsp; begin<BR>&nbsp;&nbsp;&nbsp;set @b = substring(@name,0,charindex(',',@name))<BR>&nbsp;&nbsp;&nbsp;set @name = substring(@name,charindex(',',@name)+1,len(@name))<BR>&nbsp;&nbsp;&nbsp;print @b<BR>&nbsp;&nbsp;&nbsp;&nbsp; fetch mycursor into @name<BR>&nbsp;&nbsp;&nbsp; end<BR>&nbsp;&nbsp; close mycursor<BR>&nbsp;&nbsp; deallocate mycursor </P>
问题补充: 为什么我这个做法好像只执行了一次,就输出个1?该怎么改?
︷起↘嚸.的主页 ︷起↘嚸. | 初学一级 | 园豆:125
提问于:2008-04-18 15:02
< >
分享
所有回答(2)
0
WHILE @@FETCH_STATUS=0 为啥要if @@fetch_status <>-2?
风海迷沙 | 园豆:4453 (老鸟四级) | 2008-04-18 17:10
0
declare mycursor cursor for select @name中,select @name的结果只有一条,因此只执行一次
MyFavorite | 园豆:159 (初学一级) | 2008-04-19 11:33
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册