oracle 错误(26,14):PLS-00103:在期望以下之一时遇到符号 <cursor>: := 。(@%;
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30664426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error(26,14): PLS-00103: Encountered the symbol <cursor> when expecting one of the following: := . ( @ % ;
提问by shirjai
I am currently new to oracle with a MSSQL knowledge. currently, i am writing down few procedures to understand the syntax . I am not able to compile the following procedure. Not sure what is going wrong . I am constantly facing the error
我目前是 oracle 的新手,具有 MSSQL 知识。目前,我正在写下一些程序来理解语法。我无法编译以下过程。不知道出了什么问题。我经常面临错误
Error(26,14): PLS-00103: Encountered the symbol "CRIT_CURSOR" when expecting one of the following: := . ( @ % ;
Below is the code:
下面是代码:
create or replace
PROCEDURE STUDY_ORA
AS
BEGIN
DECLARE
CRITVALID INTEGER;
CURSOR CRIT_CURSOR IS
SELECT ID FROM USERLIST;
BEGIN
OPEN CRIT_CURSOR;
LOOP
FETCH CRIT_CURSOR INTO CRITVALID;
EXIT WHEN CRIT_CURSOR%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(CRITVALID);
END LOOP;
CLOSE CRIT_CURSOR ;
END;
CURSOR CRIT_CURSOR IS
SELECT ID FROM TXSLIST;
BEGIN
OPEN CRIT_CURSOR;
LOOP
FETCH CRIT_CURSOR INTO CRITVALID;
EXIT WHEN CRIT_CURSOR%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(CRITVALID);
END LOOP;
CLOSE CRIT_CURSOR ;
END;
END;
I would appreciate if someone can provide some insights . thanks.
如果有人能提供一些见解,我将不胜感激。谢谢。
回答by davegreen100
your problem is that the second cursor is not declared within a DECLARE block, you will also need to redeclare the variable you are using (CRITVALID)
您的问题是第二个游标未在 DECLARE 块中声明,您还需要重新声明您正在使用的变量 (CRITVALID)
create or replace
PROCEDURE STUDY_ORA
AS
BEGIN
DECLARE
CRITVALID INTEGER;
CURSOR CRIT_CURSOR IS
SELECT ID FROM USERLIST;
BEGIN
OPEN CRIT_CURSOR;
LOOP
FETCH CRIT_CURSOR INTO CRITVALID;
EXIT WHEN CRIT_CURSOR%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(CRITVALID);
END LOOP;
CLOSE CRIT_CURSOR ;
END;
declare
CRITVALID INTEGER;
CURSOR CRIT_CURSOR IS
SELECT ID FROM TXSLIST;
BEGIN
OPEN CRIT_CURSOR;
LOOP
FETCH CRIT_CURSOR INTO CRITVALID;
EXIT WHEN CRIT_CURSOR%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(CRITVALID);
END LOOP;
CLOSE CRIT_CURSOR ;
END;
END;
回答by Alex Poole
You're missing a DECLARE
before your second declaration of that cursor:
您在DECLARE
该游标的第二次声明之前缺少一个:
..
END;
DECLARE
CURSOR CRIT_CURSOR IS
SELECT ID FROM TXSLIST;
BEGIN
...
And CRITVALID
is only in scope for your first inner block; you either need to redeclare that too, or move its declaration to the procedure level (i.e. before the very first BEGIN
for the procedure itself).
并且CRITVALID
仅在您的第一个内部块的范围内;您也需要重新声明它,或者将其声明移至过程级别(即在BEGIN
过程本身的第一个之前)。
Using implicit cursors would be simpler than having separate blocks and repeated (scoped) names. It's unusual (I think) to have nested blocks unless you need to capture an exception. They don't really add anything to flow here, apart from allowing you to redefine the cursor.
使用隐式游标比使用单独的块和重复(作用域)名称更简单。除非您需要捕获异常,否则嵌套块是不寻常的(我认为)。除了允许您重新定义光标之外,它们并没有真正添加任何内容来流动。
回答by Bob Jarvis - Reinstate Monica
To expand on @AlexPoole's suggestion to use implicit cursors, I suggest rewriting your procedure as:
为了扩展@AlexPoole 使用隐式游标的建议,我建议将您的过程重写为:
CREATE OR REPLACE PROCEDURE STUDY_ORA AS
BEGIN
FOR row1 IN (SELECT ID FROM USERLIST)
LOOP
DBMS_OUTPUT.PUT_LINE(row1.ID);
END LOOP;
FOR row2 IN (SELECT ID FROM TXSLIST)
LOOP
DBMS_OUTPUT.PUT_LINE(row2.ID);
END LOOP;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error in STUDY_ORA: ' || SQLCODE || ' : ' || SQLERRM);
RAISE;
END STUDY_ORA;
I think this version is not only shorter but much clearer. I also added an exception block so that in the case that an exception is raised you'll get a reasonable report of the error which occurred.
我认为这个版本不仅更短,而且更清晰。我还添加了一个异常块,以便在引发异常的情况下,您将获得发生错误的合理报告。