Stored procedures 是否需要在PL/SQL中背靠背地运行多个过程,以便创建一个表,然后按照代码中的结构填充?

Stored procedures 是否需要在PL/SQL中背靠背地运行多个过程,以便创建一个表,然后按照代码中的结构填充?,stored-procedures,plsql,oracle-sqldeveloper,Stored Procedures,Plsql,Oracle Sqldeveloper,我正在与Oracle SQL Developer合作,我正在尝试让下面的代码正常工作,但就是想不出来。我尝试了多种不同的方法,包括实现for循环、执行immediate、调度和重新编译 BEGIN ORDER_STATUS_1_DROP_TABLE; -- If the table exist, drop it ORDER_STATUS_2_CREATE_TABLE; -- Create the table GRANT_NEWANALYTICS; -- G

我正在与Oracle SQL Developer合作,我正在尝试让下面的代码正常工作,但就是想不出来。我尝试了多种不同的方法,包括实现for循环、执行immediate、调度和重新编译

BEGIN
  ORDER_STATUS_1_DROP_TABLE;    -- If the table exist, drop it
  ORDER_STATUS_2_CREATE_TABLE;  -- Create the table
  GRANT_NEWANALYTICS;           -- Grant users select access
  ORDER_STATUS_3_SCRIPT;        -- Run script to insert data into table
END;
代码试图做的是:

步骤1:如果存在,则删除,否则跳过。如果在没有表的情况下运行此过程,我不希望看到任何警告错误,表明不存在表。本程序本身并不起预期作用

create or replace PROCEDURE ORDER_STATUS_1_DROP_TABLE IS 
    table_does_not_exist EXCEPTION;
    PRAGMA EXCEPTION_INIT(table_does_not_exist, -942);
  BEGIN
    EXECUTE IMMEDIATE 'DROP TABLE <Table Name>';
  EXCEPTION
    WHEN table_does_not_exist then
    dbms_output.put_line( 'table dose not exist');
  END ORDER_STATUS_1_DROP_TABLE;
我得到以下错误报告:

Error report -
ORA-04068: existing state of packages has been discarded
ORA-04065: not executed, altered or dropped stored procedure "<user>.ORDER_STATUS_3_SCRIPT"
ORA-06508: PL/SQL: could not find program unit being called: "<user>.ORDER_STATUS_3_SCRIPT"
ORA-06512: at line 5
04068. 00000 -  "existing state of packages%s%s%s has been discarded"
*Cause:    One of errors 4060 - 4067 when attempt to execute a stored procedure.
*Action:   Try again after proper re-initialization of any application's state.
现在,如果我单独运行这些,它会工作。因此,如果我首先运行这个:

 BEGIN
  ORDER_STATUS_1_DROP_TABLE;    -- If the table exist, drop it
  ORDER_STATUS_2_CREATE_TABLE;  -- Create the table
  GRANT_NEWANALYTICS;           -- Grant users select access
END;
<OUTPUT> PL/SQL procedure successfully completed.
然后这个:

BEGIN
ORDER_STATUS_3_SCRIPT;        -- Run script to insert data into table
END;
<OUTPUT> <Query runs>

我没有问题。我想在一次扫描中运行这些程序集,并且在这样的想法上需要一些帮助。有人有什么想法吗

如果希望将所有这些过程作为单个PL/SQL块的一部分运行,那么对表的每个引用都需要通过动态SQL。所以ORDER_STATUS_3_脚本需要使用动态SQL来构建insert语句来填充表,而不是使用简单的静态SQL。这显然是可能的,但它确实增加了脚本的复杂性。可能是实质性的


拥有两个PL/SQL块(您已经演示过了)似乎简单得多。

为什么要删除表?最好使用包含上述过程和动态SQL的包,因为@Justin Cave建议删除和重新创建表在Oracle中基本上是一种反模式,从来没有必要。它可以将此用作一次性使用表。在重新填充之前,3_脚本将删除所有行。因此,删除和重新创建与insead 3_脚本中的truncate相比没有任何优势。这将使您的整个过程简化为1脚本。另一个选项是创建一个全局临时表,这取决于3_脚本之后处理数据的方式。无论哪种方式,订单状态脚本都是您唯一需要的脚本。嗨,贾斯汀。是的,我明白了。有时某些源字段无法从上游源获得,我这样设计,以便在过程中可以注释掉当月不可用的任何字段。也许我可以想一想,若字段不可用,那个么就为空。我得考虑一下。谢谢你的反馈!
  BEGIN
   ORDER_STATUS_1_DROP_TABLE;    -- If the table exist, drop it
   ORDER_STATUS_2_CREATE_TABLE;  -- Create the table
   GRANT_NEWANALYTICS;           -- Grant users select access
   ORDER_STATUS_3_SCRIPT;        -- Run script to insert data into table
  END;
Error report -
ORA-04068: existing state of packages has been discarded
ORA-04065: not executed, altered or dropped stored procedure "<user>.ORDER_STATUS_3_SCRIPT"
ORA-06508: PL/SQL: could not find program unit being called: "<user>.ORDER_STATUS_3_SCRIPT"
ORA-06512: at line 5
04068. 00000 -  "existing state of packages%s%s%s has been discarded"
*Cause:    One of errors 4060 - 4067 when attempt to execute a stored procedure.
*Action:   Try again after proper re-initialization of any application's state.
 BEGIN
  ORDER_STATUS_1_DROP_TABLE;    -- If the table exist, drop it
  ORDER_STATUS_2_CREATE_TABLE;  -- Create the table
  GRANT_NEWANALYTICS;           -- Grant users select access
END;
<OUTPUT> PL/SQL procedure successfully completed.
BEGIN
ORDER_STATUS_3_SCRIPT;        -- Run script to insert data into table
END;
<OUTPUT> <Query runs>