如何为游标语句调试open?

时间:2013-09-11 09:24:44

标签: sql oracle debugging

我想知道是否有可能以简单的方式调试类似的语句。 当我在变量中保存“选择字符串”时,它变为“长”,我需要将其拆分为更多变量。我提出了非常简化的样本:

OPEN o_recordset FOR
    'SELECT distinct
      a, b, c
  FROM t1,t2

  WHERE'
     || CASE  
     WHEN i_use_ctr_id = 1  then ' a = b' 
   END
   || ' ORDER BY 1 ASC  , DECODE('''||i_sort_order||''',null, '''', ''a'', '' NULLS LAST '',  ''b'' ,'',2 ASC NULLS LAST'')' 
   ;    

我希望看到像这样的选择(i_use_ctr_id = 1,i_sort_order = a)

  SELECT distinct
      a, b, c
  FROM t1,t2

  WHERE a = b 
   END
  ORDER BY 1 ASC  , DECODE('a',null, '''', ''a'', '' NULLS LAST '',  ''b'' ,'',2 ASC NULLS LAST'')' 
   ;    

1 个答案:

答案 0 :(得分:2)

使用调试过程,该过程可以写入文件或插入表中(使用自治事务)。

例如:

CREATE TABLE debug_t (ts timestamp default systimestamp, data VARCHAR2(4000));
CREATE OR REPLACE PROCEDURE debug_p (p VARCHAR2) IS
   PRAGMA autonomous_transaction;
BEGIN
   -- you should split p if length is > 4000
   INSERT INTO debug_t (data) VALUES (p);
   COMMIT;
END;
/

然后您可以通过插入一行代码来调试值:

SQL> DECLARE
  2     l_sql VARCHAR2(4000);
  3     i_use_ctr_id NUMBER;
  4     i_sort_order NUMBER;
  5  BEGIN
  6      l_sql := 'SELECT distinct
  7        a, b, c
  8    FROM t1,t2
  9  
 10    WHERE'
 11       || CASE
 12       WHEN i_use_ctr_id = 1  then ' a = b'
 13     END
 14     || ' ORDER BY 1 ASC  , DECODE('''||i_sort_order
 15     ||''',null, '''', ''a'', '' NULLS LAST '',  ''b'' ,'',2 ASC NULLS LAST'')'
 16     ;
 17     debug_p(l_sql); -- debug before opening cursor
 18  END;
 19  /

PL/SQL procedure successfully completed

SQL> select * from debug_t;

TS                DATA
----------------- --------------------------------------------------------------------------------
11/09/13 11:52:30 SELECT distinct
                        a, b, c
                    FROM t1,t2

                    WHERE ORDER BY 1 ASC  , DECODE('',null, '', 'a', ' NULLS LAST ',  'b' ,',2 ASC