【问题标题】:PL/SQL: Execute procedure in another procedurePL/SQL:在另一个过程中执行过程
【发布时间】:2019-03-26 03:19:47
【问题描述】:

我正在另一个过程中执行一个过程。

程序 1:

CREATE OR REPLACE PROCEDURE proc_test_status_table(
     p_test_description IN VARCHAR2,
     p_test_status IN varchar2)
   AS
   l_sql VARCHAR2(4000);
  BEGIN
  l_sql := 'insert into test_status_table(test_description, test_status)
            values
            ( '''||p_test_description||''',
              '''||p_test_status||''')';

  EXECUTE IMMEDIATE (l_sql);

END;
/

程序 2:

overriding member procedure after_calling_test(self in out nocopy ut_documentation_reporter, a_test ut_test) as
    l_message varchar2(4000);
    l_test_description VARCHAR2(1000);
    l_test_status VARCHAR2(100);
  begin
    l_message := coalesce(a_test.description, a_test.name)||' ['||round(a_test.execution_time,3)||' sec]';
    Dbms_Output.Put_Line(a_test.result||'test_result');
    --if test failed, then add it to the failures list, print failure with number
    if a_test.result = ut_utils.gc_disabled then
      self.print_yellow_text(l_message || ' (DISABLED)');
      l_test_description := 'DISABLED';
      proc_test_status_table(l_message, l_test_description);

    elsif a_test.result = ut_utils.gc_success then
      self.print_green_text(l_message);
      l_test_description := 'PASS';
      proc_test_status_table(l_message, l_test_description);


    elsif a_test.result > ut_utils.gc_success then
      failed_test_running_count := failed_test_running_count + 1;
      self.print_red_text(l_message || ' (FAILED - ' || failed_test_running_count || ')');
      l_test_description := 'FAIL';
      proc_test_status_table(l_message, l_test_description);

    end if;

    -- reproduce the output from before/after procedures and the test
    self.print_clob(a_test.get_serveroutputs);
  end;

它不会将消息和描述存储在 test_status_table 表中,但是当我打印它们时它会显示出来。

我是不是做错了什么?

【问题讨论】:

  • 您已经得到了答案(由 user7294900 发布)。但是,为什么在 proc_test_status_table 过程中使用动态 SQL?里面没有任何动态的

标签: oracle unit-testing stored-procedures plsql procedure


【解决方案1】:

您可能只需要在过程中提交您记录的消息。

日志记录是自主事务有效的少数情况之一:通常我们希望在不干扰我们正在记录的事务的情况下记录我们的消息。

此外,您不需要在此处使用动态 SQL。只需引用 VALUES 子句中的参数即可。

CREATE OR REPLACE PROCEDURE proc_test_status_table(
     p_test_description IN VARCHAR2,
     p_test_status IN varchar2)
AS
    l_sql VARCHAR2(4000);
    PRAGMA autonomous_transaction;
BEGIN
    insert into test_status_table(test_description, test_status)
    values ( p_test_description, p_test_status);
    commit;
END;
/ 

这里 AUTONOMOUS_TRANSACTION 的值是多少? OP 似乎正在构建一个测试框架。使用自治事务,我们可以在不影响更广泛事务(即测试)的情况下保留日志消息。在没有 AUTONOMOUS_TRANSACTION pragma 的情况下提交日志消息可能会产生副作用,这可能会破坏其他测试(例如 ORA-01022、ORA-1555),或者可能会使拆卸更加复杂。

【讨论】:

    【解决方案2】:

    您缺少commit; 声明

    直接路径插入后:在插入提交之前无法读取表。

    在程序末尾添加提交以查看插入的记录

    【讨论】:

      【解决方案3】:

      你忘记提交了。如果要将其存储到表中,则必须在每个插入语句之后提交。

      【讨论】:

      • @SuccessShrestha - 当然,您有权选择您接受的任何答案。但我认为你应该解释为什么你不接受我的答案,而支持一个只是我答案子集的答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-08
      • 2013-03-26
      • 1970-01-01
      • 2011-09-06
      • 2011-07-09
      相关资源
      最近更新 更多