读书人

游标使用异常

发布时间: 2012-03-19 22:03:05 作者: rapoo

游标使用错误
delimiter $$
drop procedure if exists sp_c $$
create procedure sp_c (out oMax int)
begin
declare cur1 cursor for select eid from emp;
declare iMax,iTemp int;
declare i int;
set i=1;
open cur1;
fetch cur1 into iTemp;
set iMax=iTemp;
while i < 500 do
fetch cur1 into iTemp;
if iTemp> iMax then
set iMax=iTemp;
end if;
end while;
close cur1;
set oMax=iMax;
end$$
delimiter ;

我刚学习游标的使用.这里主要有什么问题?还有.哪里有游标的例子啊?

[解决办法]
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

USE pubs
IF EXISTS (SELECT name FROM sysobjects
WHERE name = 'sp_c ' AND type = 'P ')
DROP PROCEDURE sp_c
GO


create procedure sp_c
(
@oMax int
)
AS
BEGIN
-- CURSOR 物件提供, 以保存 CURSOR 物件前行位值
DECLARE @nMax int
DECLARE rs_cursor CURSOR FOR
select eid from emp

OPEN rs_cursor
FETCH NEXT FROM rs_cursor INTO @nMax

WHILE @@FETCH_STATUS = 0
BEGIN
-------------在面做你想要做的事情
FETCH NEXT FROM rs_cursor INTO @CP_NO
END
CLOSE rs_cursor
DEALLOCATE rs_cursor

-- 返回料集
ReturnData:

-- 事完成, 退出存程序
RETURN 0
-- 理
ErrorHandler:
GOTO ReturnData
END


GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

读书人网 >Mysql

热点推荐