Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Oracle 执行立即和缺少无效选项_Oracle_Dynamic Sql - Fatal编程技术网

Oracle 执行立即和缺少无效选项

Oracle 执行立即和缺少无效选项,oracle,dynamic-sql,Oracle,Dynamic Sql,该过程用于克隆表。它接收参数中两个表的名称,调用时应克隆该表 CREATE OR REPLACE PROCEDURE CLONE_TABLE( table_source VARCHAR2, table_destination VARCHAR2) is begin execute immediate 'create table ' || table_destination || 'as select* from ' || table_source; end; 但是当我调用该过程时,我收到错误

该过程用于克隆表。它接收参数中两个表的名称,调用时应克隆该表

CREATE OR REPLACE PROCEDURE CLONE_TABLE(
table_source VARCHAR2,
table_destination  VARCHAR2)
is
begin
execute immediate 'create table ' || table_destination || 'as select* from ' 
|| table_source;
end;
但是当我调用该过程时,我收到错误
ORA-00922:缺少或无效选项

BEGIN
CLONE_TABLE('example','example_new')
END

我不知道问题出在哪里,该如何解决。

as select..
之前和
select
*
附近的
select*
之间应该有一个空格

SQL> CREATE OR REPLACE PROCEDURE CLONE_TABLE(
table_source VARCHAR2,
table_destination  VARCHAR2)
is
begin
execute immediate 'create table ' || table_destination || ' as select * from ' || table_source;
end;
/  2    3    4    5    6    7    8  

Procedure created.

SQL> create table example(id number);

Table created.

SQL> exec CLONE_TABLE('example','example_new');

PL/SQL procedure successfully completed.
使用以下命令:

CREATE OR REPLACE PROCEDURE CLONE_TABLE (table_source         VARCHAR2,
                                         table_destination    VARCHAR2)
IS
v_sql varchar2(1000);
BEGIN

 v_sql:=  'create table '
      || table_destination
      || ' as select * from '
      || table_source;

 dbms_output.put_line(v_sql);

 EXECUTE IMMEDIATE v_sql;

END;

处理类似问题的最佳方法是首先显示您试图立即执行的内容。你会知道你的错

编写动态SQL很难,因为应该是编译错误的内容会变成运行时错误。因此,在查看自己的代码时,培养冷静的眼光是至关重要的。你必须是编译器

它有助于首先将语句组装为变量。这样,在出现错误时可以显示语句,从而使调试更容易

CREATE OR REPLACE PROCEDURE CLONE_TABLE(
    table_source VARCHAR2,
    table_destination  VARCHAR2)
is
    stmt varchar2(32767);
begin
    stmt := 'create table ' || table_destination || 'as select* from ' 
|| table_source;
    execute immediate stmt;
exception
    when others then
         dbms_output.put_line(stmt);
         raise;
end;
如果您这样做了,那么很明显,
as
前面缺少了一个空格。所以你的执行声明是这样的:

 create table example_newas select* from example 

select
*
之间的空格是可选的,但是如果有一个空格,代码看起来会更好。

对于您的代码,只有两个问题和两个建议

  • 选择时,
    前面缺少空格…
  • 调用过程时,使用
    exec
    call
    语句

    建议:

    • 确保进行正确的错误处理(因此,如果表不存在,或者如果表目标已经存在,您应该得到正确的返回消息)

    • 还可以按照其他答案的建议,用空格分隔每个关键字,例如在
      选择
      *
      之间。但不这样做不会给您带来错误

      CREATE OR REPLACE PROCEDURE CLONE_TABLE(
      table_source VARCHAR2,
      table_destination  VARCHAR2)
      is
      begin
      execute immediate 'create table ' || table_destination || ' as select* from ' 
      || table_source;
      end;
      
      EXEC CLONE_TABLE('example','example_new')
      

  • 感谢您提供异常处理部分。我不太确定如何在我的答案中对其进行编码。