创建 Oracle 过程
我正在尝试创建一个过程,并且它创建时没有错误。但是,当我尝试运行它时,我会收到以下错误。请告知
SQL> begin
2 Update_STUD_Fin ( '1011');
3 end;
4 /
begin
*
ERROR at line 1:
ORA-06511: PL/SQL: cursor already open
ORA-06512: at "ORAIN.UPDATE_STUD_FIN", line 3
ORA-06512: at "ORAIN.UPDATE_STUD_FIN", line 8
ORA-06512: at line 2
程序是
SQL> CREATE OR REPLACE PROCEDURE Update_STUD_Fin ( AIDY_CODE IN VARCHAR2 ) IS
2 CURSOR PublicationC IS
3 SELECT SGidm from SGB
4 WHERE SGCODE_EFF ='201030';
5 BEGIN
6 OPEN PublicationC;
7
8 FOR PublicationR IN PublicationC
9 LOOP
10 DBMS_OUTPUT.PUT_LINE( PublicationR.SGidm );
11 END LOOP;
12
13 close PublicationC;
14
15 END;
16 /
Procedure created.
I am trying to create a procedure and it created without error. However when I try to run it then I will get following error. Please advise
SQL> begin
2 Update_STUD_Fin ( '1011');
3 end;
4 /
begin
*
ERROR at line 1:
ORA-06511: PL/SQL: cursor already open
ORA-06512: at "ORAIN.UPDATE_STUD_FIN", line 3
ORA-06512: at "ORAIN.UPDATE_STUD_FIN", line 8
ORA-06512: at line 2
The Procedure is
SQL> CREATE OR REPLACE PROCEDURE Update_STUD_Fin ( AIDY_CODE IN VARCHAR2 ) IS
2 CURSOR PublicationC IS
3 SELECT SGidm from SGB
4 WHERE SGCODE_EFF ='201030';
5 BEGIN
6 OPEN PublicationC;
7
8 FOR PublicationR IN PublicationC
9 LOOP
10 DBMS_OUTPUT.PUT_LINE( PublicationR.SGidm );
11 END LOOP;
12
13 close PublicationC;
14
15 END;
16 /
Procedure created.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您不能显式打开游标并在隐式 FOR 循环中使用它。您可以选择隐式(FOR 循环)或显式(OPEN/FETCH/CLOSE)。
You cannot explicity OPEN the cursor and also use it in an implicit FOR loop. You much choose either implicit (FOR loop) or explicit(OPEN/FETCH/CLOSE).
如果您使用带有 FOR/IN/LOOP 的游标,则无需显式打开它。只需写:
If you use a cursor with a FOR/IN/LOOP, you don't need to open it explictly. Just write:
6512 错误消息指示 PL-SQL 代码中导致错误的行号。
您确定之前没有收到其他消息吗?
6512 Error message indicates the line number in PL-SQL code that the error resulted.
Are you sure you don't have another message before ?
我不知道为什么您会收到 ORA-06512 消息,而上面没有其他错误消息。您确定已粘贴完整的错误消息吗?
ORA-06512 仅在 PL/SQL 代码中引发异常时打印堆栈跟踪时使用。这是一个完整的示例:
这里真正的错误是 ORA-01001。 ORA-06512 仅说明错误发生的位置:在 USER.SOME_PROCEDURE 的第 5 行,该错误由匿名 PL/SQL 块的第 1 行调用。
查看您的代码,我可以发现几个问题:
当使用
FOR ... IN some_cursor LOOP ...
时,您不应该显式打开和关闭游标。这将由FOR
循环自动为您完成。您也不应该尝试关闭尚未打开的游标。如果您尝试这样做,您将收到 ORA-01001“无效游标”错误消息。我猜您已将其放入以确保游标在打开之前已关闭,但不幸的是您不能这样做。
简而言之,您应该从过程中删除所有
OPEN
和CLOSE
语句,然后重试。I don't know why you're getting an ORA-06512 message with no other error message above it. Are you sure you have pasted the entire error message?
ORA-06512 is only used when printing out stack traces when an exception is raised in PL/SQL code. Here's a complete example:
The real error here is the ORA-01001. The ORA-06512s merely state where the error occurred: on line 5 of
USER.SOME_PROCEDURE
, which was called by line 1 of an anonymous PL/SQL block.Taking a look at your code, I can spot a couple of problems:
When using
FOR ... IN some_cursor LOOP ...
then you should not explicitly open and close the cursor. That will be done for you automatically by theFOR
loop.You should also not attempt to close a cursor that hasn't been opened. If you try to do this, you'll get an ORA-01001 'invalid cursor' error message. I'm guessing you've put this in to ensure that the cursor was closed before you opened it, but unfortunately you can't do that.
In short, you should delete all of the
OPEN
andCLOSE
statements from your procedure and try again.