看一下我的游标@@CURSOR_ROWS,怎么返回-1啊

kerisyml 2008-10-08 09:59:27
这是我的存储过程,调试的时候,发现@@CURSOR_ROWS总是-1,可是我那条sql语句单独查找,有7条记录的,求教。
另外请大家帮我看看我的存储过程的事务、出错、提交,传出参数这样写对吗?

CREATE PROCEDURE sp_event_autopm
@nRc VARCHAR(1) OUTPUT, -- 返回ID(0-成功;1-错误)
@sMsg VARCHAR(128) OUTPUT -- 返回信息
AS
-- 变量定义
declare @v_eventid varchar(9); -- 事件ID
declare @v_dqrq varchar(6); -- 当前日期(yymmdd)
declare @v_sjxl varchar(3); -- 事件序列

declare @v_azbh varchar(10); -- 安装单编号
declare @v_jqbh varchar(20); -- 机器编号

BEGIN

BEGIN TRAN tt
-- 定义游标
declare cur_event_autopm cursor for select sazbh,sjqbh from z_machine_info where sjqzt='0' and
cast(sxjsj as int)-cast(convert(char(8),getdate(),112) as int)<=7;

-- 打开游标
open cur_event_autopm;

-- 如游标记录数为0,则退出该游标
IF @@CURSOR_ROWS = 0 GOTO END_cur_event_autopm

DECLARE @I INT
DECLARE @J INT
SELECT @I=0

while @I < @@CURSOR_ROWS
begin
IF @I=0
fetch FIRST from cur_event_autopm into @v_azbh,@v_jqbh
ELSE
fetch next from cur_event_autopm into @v_azbh,@v_jqbh

-- 自动生成一个事件ID
select @v_dqrq = substring(convert(char(8),getdate(),112),3,6)
select @v_sjxl = ssjxl from f_system;
select @v_eventid = @v_dqrq + @v_sjxl;
if @v_sjxl = '999'
update f_system set ssjxl='001';
else
update f_system set ssjxl=right('000'+rtrim(ssjxl+1),3);

-- 错误跳出
SELECT @J=@@ERROR
if @j<> 0 GOTO END_cur_event_autopm


-- 根据"安装单编号"来新增一条新的PM事件
insert into z_event(seventid,ssjlb,sjqbh,sazbh,ssjzt,sczry,sczrq)
values(@v_eventid,'3',@v_jqbh,@v_azbh,'1','0001',replace(replace(replace(convert(varchar,getdate(),120),'-',''),':',''),' ',''));
-- 错误跳出
SELECT @J=@@ERROR
if @j<> 0 GOTO END_cur_event_autopm


-- 更新“巡检时间”为加上原巡检时间再加上三个月
update z_machine_info set sxjsj=convert(char(8),dateadd(mm,3,convert(datetime,sxjsj)),112) where sazbh=@v_azbh;
-- 错误跳出
SELECT @J=@@ERROR
if @j<> 0 GOTO END_cur_event_autopm

-- 错误跳出
SELECT @J=@@ERROR
if @j<> 0 GOTO END_cur_event_autopm

--循环递增
SELECT @I=@I+1
end

END_cur_event_autopm:
close cur_event_autopm
DEALLOCATE cur_event_autopm

IF @J=0
BEGIN
COMMIT TRAN tt
set @nRc = 0;
set @sMsg = '触发性PM的自动生成成功!';
RETURN
END
ELSE
BEGIN
ROLLBACK TRAN tt
set @nRc = 1;
set @sMsg = '触发性PM的自动生成发生错误!';
END

END
GO
...全文
489 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
中国风 2008-10-08
  • 打赏
  • 举报
回复
樓主應看一下游標的用法比較好..
cyc_cheng 2008-10-08
  • 打赏
  • 举报
回复

-m
游标被异步填充。返回值 (-m) 是键集中当前的行数。

-1
游标为动态游标。因为动态游标可反映所有更改,所以游标符合条件的行数不断变化。因此,永远不能确定已检索到所有符合条件的行。

0
没有已打开的游标,对于上一个打开的游标没有符合条件的行,或上一个打开的游标已被关闭或被释放。

n
游标已完全填充。返回值 (n) 是游标中的总行数。

beckham2537 2008-10-08
  • 打赏
  • 举报
回复
游标类型的问题
定义成: cursor scroll的
declare aaa cursor scroll for select * from syscolumns
open aaa


select @@CURSOR_ROWS
close aaa
deallocate aaa
给分吧
zcyan666 2008-10-08
  • 打赏
  • 举报
回复
试下:
-- 定义游标
declare cur_event_autopm cursor for
select sazbh,sjqbh,,@@CURSOR_ROWS from z_machine_info where sjqzt='0' and
cast(sxjsj as int)-cast(convert(char(8),getdate(),112) as int) <=7;
Garnett_KG 2008-10-08
  • 打赏
  • 举报
回复
静态CURSOR 才能获取到@@CURSOR_ROWS
Try:


-- 定义游标
declare cur_event_autopm cursor STATIC
for select sazbh,sjqbh from z_machine_info where sjqzt='0' and
cast(sxjsj as int)-cast(convert(char(8),getdate(),112) as int) <=7;


kerisyml 2008-10-08
  • 打赏
  • 举报
回复
rucypli:
能这样加吗,我加了好像报错的。
open cur_event_autopm into @v_azbh,@v_jqbh
rucypli 2008-10-08
  • 打赏
  • 举报
回复
open cur_event_autopm;
后面加cur_event_autopm into @v_azbh,@v_jqbh
中国风 2008-10-08
  • 打赏
  • 举报
回复

@@CURSOR_ROWS
-m
非同步地擴展資料指標。傳回的值 (-m) 是目前在索引鍵集中的資料列數。

-1
資料指標是動態的。由於動態資料指標會反映所有變更,因此,資料指標之符合的資料列數會不斷改變。永遠不可能明確指出已擷取了所有符合的資料列。

0
未開啟任何資料列、最後開啟的資料指標沒有適合的資料列,或最後開啟的資料指標已關閉或取消配置。

n
已充分擴展資料指標。傳回的值 (n) 是資料指標中的總資料列數。

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧