2013-03-25 40 views
3

与执行直接和错误处理做约两个小时的价值的研究上的各种问题之后,我不认为我遇到的正是我要提出的问题。我已将问题简化为下面的示例。问题是:甲骨文执行DDL立即模式和错误

当我执行一个程序创建即时模式在变量中创建文本和语法错误,在创建文本时,执行立即调用抛出,表明它失败的错误。但我无法了解潜在的错误或其文本。

然而,当我在sqlplus直接创建相同PROC(W/O中间模式)时,得到错误完美。

那么,如何在一个导致即时模式失败的错误得到什么?

我已经穿插的输出与我的评论(MOP),减少的空白行数:

SQL*Plus: Release 10.2.0.1.0 - Production on Mon Mar 25 15:57:06 2013 
Copyright (c) 1982, 2005, Oracle. All rights reserved. 
Connected to: 
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production 
With the Partitioning, OLAP, Data Mining and Real Application Testing options 

SQL> set serveroutput on 

SQL> declare 
    2 eSQL varchar(568); 
    3 begin 
    4  eSQL := 'create or replace procedure MOPMOP 
    5 as 
    6 begin 
    7  select 1 from dual; 
    8 end; 
    9 '; 
10  execute immediate eSQL; 
11 end; 
12/
ERROR: 
ORA-24344: success with compilation error 
ORA-06512: at line 10 
Warning: PL/SQL compilation errors. 

这是一件好事。有一个编译错误。所以,现在让我问一下那个编译错误是什么:

SQL> show errors; 
No errors. 

这是不行的。有一个编译错误。它以前如何!?!?!?与此相反:

SQL> create or replace procedure MOPMOP 
    2 as 
    3 begin 
    4  select 1 from dual; 
    5 end; 
    6/

Warning: Procedure created with compilation errors. 

这很好。有一个编译错误。所以,现在让我来问一下那个编译错误是什么:

SQL> show errors; 
Errors for PROCEDURE MOPMOP: 

LINE/COL ERROR 
-------- ----------------------------------------------------------------- 
4/5 PLS-00428: an INTO clause is expected in this SELECT statement 
SQL> 

这很好。有一个编译错误。现在我知道这是什么。

那么,如何才能获得中间模式吐出底层(00428)错误?

回答

0

不能使用在PL/SQL中选择1从双...。必须使用select into。在这两种情况下,您都在创建PL/SQL过程而不是某个SQL脚本。

declare 
    x number; 
begin 
    select 1 into x from dual; 
end; 
/

declare 
    procedure MOPMOP 
    as 
    x number; 
begin 
    select 1 into x from dual; 
    dbms_output.put_line(x); 
end MOPMOP; 
begin 
MOPMOP; 
end; 
/

避免ORA-24344 - 虽然我可能永远不会使用动态SQL这样的:

declare 
    eSQL varchar(568); 
    x number; 
begin 
    eSQL := 'create or replace procedure MOPMOP as 
      begin 
      select 1 into x from dual; 
      end;';    
    execute immediate eSQL; 
    EXCEPTION WHEN OTHERS THEN NULL; 
end; 
/
+0

我认为他的代码只是说明的需要,使我们能够重现问题。 – 2013-03-25 22:59:49

+0

当然...我希望他学会了关于显示错误... – Art 2013-03-26 13:15:41

+0

是的,这只是一个repro。 – mpersico 2013-03-26 16:48:34

2

当你在自己的运行show errors它会显示错误的任何先前的“创造” /“改变”是在sqlplus本身一样,这是这种情况下,没有(因为它是从sqlplus中隐藏,因为动态SQL的)

你必须expcitly说你想看到程序的错误,这种情况:

SQL> declare 
    2 eSQL varchar(568); 
    3 begin 
    4  eSQL := 'create or replace procedure MOPMOP 
    5 as 
    6 begin 
    7  select 1 from dual; 
    8 end; 
    9 '; 
10  execute immediate eSQL; 
11 end; 
12/
ERROR: 
ORA-24344: success with compilation error 
ORA-06512: at line 10 



Warning: PL/SQL compilation errors. 

SQL> show errors procedure mopmop 
Errors for PROCEDURE MOPMOP: 

LINE/COL ERROR 
-------- ----------------------------------------------------------------- 
4/5  PLS-00428: an INTO clause is expected in this SELECT statement 
SQL> 

作为创建运行服务器端而不是通过sqlplus,sqlplus不知道失败,所以不能正确地获取错误。

此外,如果你有一个先前失败的,它会保留该:

SQL> create or replace procedure MOPMOP2 
    2 as 
    3 begin 
    4  select 1 from dual; 
    5 end; 
    6/

Warning: Procedure created with compilation errors. 

SQL> declare 
    2 eSQL varchar(568); 
    3 begin 
    4  eSQL := 'create or replace procedure MOPMOP 
    5 as 
    6 begin 
    7  select 1 from dual; 
    8 end; 
    9 '; 
10  execute immediate eSQL; 
11 end; 
12/
ERROR: 
ORA-24344: success with compilation error 
ORA-06512: at line 10 



Warning: PL/SQL compilation errors. 

SQL> show errors 
Errors for PROCEDURE MOPMOP2: 

LINE/COL ERROR 
-------- ----------------------------------------------------------------- 
4/5  PLS-00428: an INTO clause is expected in this SELECT statement 
SQL> 
+0

这工作。非常感谢你的明确答复。 – mpersico 2013-03-26 16:49:25