executing `EXECUTE IMMEDIATE ` Oracle Statement Getting Error

我的未来我决定 提交于 2019-12-02 16:24:33

问题


I am NewBie to Oracle. When I Execute Following Statement

BEGIN
 EXECUTE IMMEDIATE  'SELECT * FROM DUAL;';
END;
 /

I Got Error as

Error starting at line : 2 in command - BEGIN EXECUTE IMMEDIATE 'SELECT * FROM DUAL;'; END;

Error report - ORA-00911: invalid character ORA-06512: at line 2 00911. 00000 - "invalid character" *Cause: identifiers may not start with any ASCII character other than letters and numbers. $#_ are also allowed after the first character. Identifiers enclosed by doublequotes may contain any character other than a doublequote. Alternative quotes (q'#...#') cannot use spaces, tabs, or carriage returns as delimiters. For all other contexts, consult the SQL Language Reference Manual. *Action:


回答1:


The problem is the ; character in 'SELECT * FROM DUAL;'.

From documentation:

execute_immediate_statement ::=
EXECUTE_IMMEDIATE dynamic_string
 { 
    INTO { define_variable [, define_variable ...] | record_name } 
  | BULK COLLECT INTO { collection_name [, collection_name ...] | :host_array_name } 
 }
   [ USING [ IN | OUT | IN OUT ] bind_argument
   [, [ IN | OUT | IN OUT ] bind_argument] ... ] [ returning_clause ] ;

... where dynamic_string is (emphasis mine):

A string literal, variable, or expression that represents a single SQL statement or a PL/SQL block. It must be of type CHAR or VARCHAR2, not NCHAR or NVARCHAR2.

Since it won't accept multiple statements unless you enclose them in a single PL/SQL block, the ; separator is not expected.


There's a better explanation at Using the EXECUTE IMMEDIATE Statement in PL/SQL:

When constructing a single SQL statement in a dynamic string, do not include a semicolon (;) at the end inside the quotation mark. When constructing a PL/SQL anonymous block, include the semicolon at the end of each PL/SQL statement and at the end of the anonymous block; there will be a semicolon immediately before the end of the string literal, and another following the closing single quotation mark.




回答2:


you can fix the error by removing ; from the dynamic query.

BEGIN
 EXECUTE IMMEDIATE  'SELECT * FROM DUAL';
END;
 /

This query is not going to return any results;

select statement in EXECUTE IMMEDIATE without into clause will be ignored.

Declare
    v_variable number;--some variable
BEGIN
     EXECUTE IMMEDIATE  'SELECT clmn FROM tbl' into v_variable;
END;
/



回答3:


Just remove ';' from the dymanic string.Try this :

BEGIN 
   EXECUTE IMMEDIATE 'SELECT * FROM DUAL'; 
END; 


来源:https://stackoverflow.com/questions/31474308/executing-execute-immediate-oracle-statement-getting-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!