简体   繁体   English

如何从Oracle存储过程中查看TOAD中的CLOB输出参数?

[英]How do I view a CLOB output parameter in TOAD from an Oracle Stored Procedure?

I have a stored procedure in a package in an Oracle database that has 2 input parameters + 1 output CLOB parameter. 我在Oracle数据库的一个包中有一个存储过程,该包具有2个输入参数+ 1个输出CLOB参数。 How do I view the output in Toad? 如何在Toad中查看输出? (Preferably with the user only having execute/select permissions) (最好是仅具有执行/选择权限的用户)

Solution: 解:

DECLARE
   my_output_parameter CLOB;
BEGIN 
   my_package.my_stored_proc(1, 2, my_output_parameter);
   DBMS_OUTPUT.PUT_LINE(my_output_parameter);
END;

Don't forget to execute as script, rather than just execute statement, and results appear in the DBMS Output window, not the datagrid. 不要忘记以脚本方式执行,而不仅仅是执行语句,结果将显示在“ DBMS输出”窗口中,而不是数据网格中。

I guess DBMS_OUTPUT.PUT_LINE has an internal line limit of 255 chars. 我猜DBMS_OUTPUT.PUT_LINE的内部行数限制为255个字符。 However it has been removed from 10g Release 2 onwards. 但是,它已从10g第2版开始删除。 You can try inserting the column data in a table and view it later on by querying that table. 您可以尝试将列数据插入表中,稍后通过查询该表来查看它。

Please refer - 请参考-

http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:146412348066 http://asktom.oracle.com/pls/asktom/f?p=100:11:0:::::P11_QUESTION_ID:146412348066

Would you consider printing the CLOB as a result set? 您是否考虑将CLOB打印为结果集? You could then use a PIPELINED function (more about them here: PIPELINED functions by Tim Hall ) which would return the CLOB line by line, take a look at the example below: 然后,您可以使用PIPELINED函数(有关它们的更多信息: Tim Hall的PIPELINED函数 ),它将逐行返回CLOB ,请看以下示例:

CREATE TABLE my_clob_tab (
  id NUMBER,
  clob_col CLOB
)
/

INSERT INTO my_clob_tab
  VALUES (1,
          to_clob('first line' || chr(10) ||
          'second line, a longer one' || chr(10) ||
          'third'))
/

CREATE OR REPLACE TYPE t_my_line_str AS TABLE OF VARCHAR2(2000)
/

CREATE OR REPLACE FUNCTION print_clob_func(p_id IN NUMBER)
  RETURN t_my_line_str PIPELINED
AS
  v_buffer VARCHAR2(32767);
  v_clob CLOB;
  v_len NUMBER;
  v_offset NUMBER := 1;
  v_line_break_pos NUMBER;
  v_amount NUMBER;
BEGIN
  SELECT clob_col
    INTO v_clob
  FROM my_clob_tab
  WHERE id = p_id;

  IF v_clob IS NOT NULL THEN
    v_len := dbms_lob.getlength(v_clob);

    WHILE v_offset < v_len
    LOOP
      v_line_break_pos := instr(v_clob, chr(10), v_offset);

      IF v_line_break_pos = 0 THEN
        v_amount := v_len - v_offset + 1;
      ELSE
        v_amount := v_line_break_pos - v_offset;
      END IF;

      dbms_lob.read(v_clob, v_amount, v_offset, v_buffer);
      v_offset := v_offset + v_amount + 1;

      PIPE ROW (v_buffer);      
    END LOOP;
  END IF;
END;
/

(the function can be changed so that it takes as a parameter the CLOB you get from your procedure) (可以更改函数,以便将您从过程中获取的CLOB用作参数)

The code reads the content of the CLOB line by line (I assumed that the line separator is CHR(10) - if you are on Windows, you can change it to CHR(10) || CHR(13) ) and PIPE s each line to the SELECT statement. 代码逐行读取CLOB的内容(我假设行分隔符为CHR(10) -如果在Windows上,则可以将其更改为CHR(10) || CHR(13) )和PIPE行到SELECT语句。

The function that reads the clob could also print the output to the standard output via dbms_output.put_line , but it would be trickier, because you'd have to take into account that standard output's maximal line length is limitied to, correct me if I'm wrong, 2000 characters, but it is doable (can't try that solution right now, unfortunately). 读取Clob的函数还可以通过dbms_output.put_line将输出打印到标准输出,但这会比较棘手,因为您必须考虑到标准输出的最大行长受到限制,如果需要错了,最多2000个字符,但这是可行的(很遗憾,目前无法尝试该解决方案)。 In the meanwhile, please check above proposal and give me some feedback if that would work for you. 同时,请检查上述建议,如果可以的话请给我一些反馈。

Back to the solution, now we can issue this SELECT statement: 回到解决方案,现在我们可以发出以下SELECT语句:

SELECT COLUMN_VALUE AS clob_line_by_line FROM TABLE(print_clob_func(1));

Which will give us the following output: 这将为我们提供以下输出:

CLOB_LINE_BY_LINE
-------------------------
first line
second line, a longer one
third

Check it at SQLFiddle: SQLFiddle example 在SQLFiddle中进行检查: SQLFiddle示例

Approach with inserting PL/SQL block and dbms_output : 插入PL / SQL块和dbms_output

DECLARE
   my_output_parameter CLOB;
BEGIN 
   my_package.my_stored_proc(1, 2, my_output_parameter);

  declare 
    vClob CLOB := my_output_parameter;
    vPos  number;
    vLen  number;
  begin
    vLen := DBMS_LOB.GetLength(vClob);
    vPos := 1;
    while vPos < vLen loop
      DBMS_OUTPUT.Put(DBMS_LOB.Substr(vCLOB, 200, vPos));
      vPos := vPos + 200;  
    end loop;
    DBMS_OUTPUT.new_line;
  end;

END;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM