动态 sql 错误 ORA-00911
Posted
技术标签:
【中文标题】动态 sql 错误 ORA-00911【英文标题】:dynamic sql error ORA-00911 【发布时间】:2016-09-23 07:44:54 【问题描述】:我已经创建了下面的简单代码,它将动态获取表名并根据输入表中给出的列创建一个表
CREATE OR REPLACE
PROCEDURE dyn_sql_sp(
p_user VARCHAR2,
p_table_name VARCHAR2,
P_NW_tbl_name VARCHAR2)
AS
d_cols VARCHAR2(2000);
CURSOR col_c
IS
SELECT column_name
||' '
||data_type
||'('
||data_length
||')' colm
FROM All_Tab_Columns
WHERE owner =UPPER(p_user)
AND table_name=UPPER(p_table_name);
BEGIN
FOR i IN col_c
LOOP
d_cols:=d_cols||i.colm||',';
END LOOP;
D_Cols:=RTRIM(REPLACE(D_Cols,'DATE(7)','DATE'),',');
dbms_output.put_line( 'CREATE TABLE '||P_Nw_Tbl_Name||' ('||d_cols||');');
EXECUTE Immediate ' CREATE TABLE '||P_Nw_Tbl_Name||' ( '||d_cols||'); ';
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line(sqlerrm||SQLCODE);
END;
但我收到错误
SQL> exec dyn_sql_sp('hr','departments','tamil');
CREATE TABLE tamil (DEPARTMENT_ID NUMBER(22),DEPARTMENT_NAME
VARCHAR2(30),MANAGER_ID NUMBER(22),LOCATION_ID NUMBER(22)
);
ORA-00911: invalid character
PL/SQL procedure successfully completed.
希望程序完全正确,我可以单独运行输出并且工作正常,但是在我动态创建相同的输出时出错
SQL> CREATE TABLE tamil (DEPARTMENT_ID NUMBER(22),DEPARTMENT_NAME
2 VARCHAR2(30),MANAGER_ID NUMBER(22),LOCATION_ID NUMBER(22));
Table created.
【问题讨论】:
【参考方案1】:问题可能在最后的';'在动态语句中:
SQL> begin
2 execute immediate 'create table tab_one(a number);';
3 end;
4 /
begin
*
ERROR at line 1:
ORA-00911: invalid character
ORA-06512: at line 2
SQL> begin
2 execute immediate 'create table tab_one(a number)';
3 end;
4 /
PL/SQL procedure successfully completed.
【讨论】:
SQL 语言没有分号。它看起来好像确实如此,因为客户端工具通常将它们用作命令终止符。以上是关于动态 sql 错误 ORA-00911的主要内容,如果未能解决你的问题,请参考以下文章
oracle: sql语句报ora-01461/ora-00911错误
插入错误:java.sql.SQLException:ORA-00911:无效字符
Oracle 动态SQL 注意细节 ORA-00911: 无效字符
Mybatis 批量更新 ORA-00911: 无效字符的错误