我在oracle数据库中创建了一个在ref-cursor中返回数据的过程,我希望它也将此游标的rowcount也作为输出变量返回。测试后,P_count变量被正确填充,但是当我试图打开光标时,ORA-01002:取消序列'错误被解雇我以前读过它,我发现问题是因为我在我的代码中使用了一个fetch语句。但直到现在我还没有发现如何解决它。感谢任何帮助,谢谢。以下是我的程序:
PROCEDURE IS_CLIENT_LOGGED_IN (
P_CLIENT_NUM Varchar2,
P_CURSOR out SYS_REFCURSOR ,
P_COUNT OUT NUMBER,
P_ERROR out Varchar2
) AS
cur_rec Varchar2(1024);
BEGIN
BEGIN
Open P_CURSOR FOR
SELECT ID
FROM tbl_registration
WHERE tbl_client_id = P_CLIENT_NUM
AND tbl_logout_date is null;
LOOP
FETCH P_CURSOR INTO cur_rec;
EXIT WHEN P_CURSOR%notfound;
P_COUNT := P_CURSOR%rowcount;--will return row number beginning with 1
END LOOP;
EXCEPTION WHEN OTHERS THEN
P_ERROR := 'Unable to select Data from tbl_registration' ||SQLERRM;
END;
END IS_CLIENT_LOGGED_IN;
答案 0 :(得分:0)
根据您的评论,程序应如下所示:
PROCEDURE IS_CLIENT_LOGGED_IN (
P_CLIENT_NUM Varchar2,
P_CURSOR out SYS_REFCURSOR ,
P_COUNT OUT NUMBER,
P_ERROR out Varchar2
) AS
cur_rec Varchar2(1024);
BEGIN
Open P_CURSOR FOR
SELECT ID
FROM tbl_registration
WHERE tbl_client_id = P_CLIENT_NUM
AND tbl_logout_date is null;
LOOP
FETCH P_CURSOR INTO cur_rec;
EXIT WHEN P_CURSOR%notfound;
P_COUNT := P_CURSOR%rowcount;--will return row number beginning with 1
END LOOP;
CLOSE P_CURSOR;
Open P_CURSOR FOR
SELECT ID
FROM tbl_registration
WHERE tbl_client_id = P_CLIENT_NUM
AND tbl_logout_date is null;
EXCEPTION WHEN OTHERS THEN
P_ERROR := 'Unable to select Data from tbl_registration' ||SQLERRM;
END IS_CLIENT_LOGGED_IN;
效率不高,但这就是你所要求的。
无论如何,我没有看到任何理由逐一增加P_COUNT
。
制作
SELECT COUNT(*) INTO P_COUNT
FROM tbl_registration
WHERE tbl_client_id = P_CLIENT_NUM
AND tbl_logout_date is null;
Open P_CURSOR FOR
SELECT ID
FROM tbl_registration
WHERE tbl_client_id = P_CLIENT_NUM
AND tbl_logout_date is null;
得到相同的。