创建 Oracle 过程

发布于 2024-10-09 10:58:22 字数 781 浏览 4 评论 0原文

我正在尝试创建一个过程,并且它创建时没有错误。但是,当我尝试运行它时,我会收到以下错误。请告知

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

明月松间行 2024-10-16 10:58:22

您不能显式打开游标并在隐式 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).

心清如水 2024-10-16 10:58:22

如果您使用带有 FOR/IN/LOOP 的游标,则无需显式打开它。只需写:

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
  8    FOR PublicationR IN PublicationC
  9    LOOP
 10      DBMS_OUTPUT.PUT_LINE( PublicationR.SGidm );
 11    END LOOP;
 12  
 15  END;
 16  /

If you use a cursor with a FOR/IN/LOOP, you don't need to open it explictly. Just write:

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
  8    FOR PublicationR IN PublicationC
  9    LOOP
 10      DBMS_OUTPUT.PUT_LINE( PublicationR.SGidm );
 11    END LOOP;
 12  
 15  END;
 16  /
帝王念 2024-10-16 10:58:22

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 ?

迷途知返 2024-10-16 10:58:22

我不知道为什么您会收到 ORA-06512 消息,而上面没有其他错误消息。您确定已粘贴完整的错误消息吗?

ORA-06512 仅在 PL/SQL 代码中引发异常时打印堆栈跟踪时使用。这是一个完整的示例:

ERROR at line 1:
ORA-01001: invalid cursor
ORA-06512: at "USER.SOME_PROCEDURE", line 5
ORA-06512: at line 1

这里真正的错误是 ORA-01001。 ORA-06512 仅说明错误发生的位置:在 USER.SOME_PROCEDURE 的第 5 行,该错误由匿名 PL/SQL 块的第 1 行调用。

查看您的代码,我可以发现几个问题:

  • 当使用 FOR ... IN some_cursor LOOP ... 时,您不应该显式打开和关闭游标。这将由 FOR 循环自动为您完成。

  • 您也不应该尝试关闭尚未打开的游标。如果您尝试这样做,您将收到 ORA-01001“无效游标”错误消息。我猜您已将其放入以确保游标在打开之前已关闭,但不幸的是您不能这样做。

简而言之,您应该从过程中删除所有 OPENCLOSE 语句,然后重试。

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:

ERROR at line 1:
ORA-01001: invalid cursor
ORA-06512: at "USER.SOME_PROCEDURE", line 5
ORA-06512: at line 1

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 the FOR 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 and CLOSE statements from your procedure and try again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文