2013-03-25 43 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