PL/SQl sqldeveloper想在plsql中输出多行

Posted

技术标签:

【中文标题】PL/SQl sqldeveloper想在plsql中输出多行【英文标题】:PL/SQl sqldeveloper want to output multiple rows in plsql 【发布时间】:2015-12-01 13:04:39 【问题描述】:

我有一个过程和匿名块,我在其中输入两个日期,它会在票证表中搜索在这两个日期期间已解决的票证并输出 em。但我似乎无法弄清楚如何在 pl/sql 中输出多行。

ORA-01422: exact fetch returns more than requested number of rows
ORA-06512: at "SYSTEM.JOBS_DONE", line 9
ORA-06512: at line 8
01422. 00000 -  "exact fetch returns more than requested number of rows"
*Cause:    The number specified in exact fetch is less than the rows returned.
*Action:   Rewrite the query or change number of rows requested

CREATE OR REPLACE PROCEDURE jobs_done(
  month_start    IN  tickets.date_reported%TYPE,
  month_end      IN  tickets.date_resolved%TYPE,
  userid         OUT tickets.user_id%TYPE,
  supportstaffid OUT tickets.support_staff_id%TYPE,
  resolution     OUT tickets.resolution_details%TYPE)
AS
  BEGIN

    SELECT user_id, support_staff_id, resolution_details
      INTO userid, supportstaffid, resolution 
      FROM tickets
     WHERE date_resolved >= month_start AND date_resolved <= month_end;

    dbms_output.put_line('userid, supportstaffid, resolution');
    dbms_output.put_line(userid || supportstaffid || resolution);

  END jobs_done;
/

SET serveroutput ON

DECLARE
  month_start    tickets.date_reported%TYPE := &Enter_date_start;
  month_end      tickets.date_resolved%TYPE := &Enter_date_end;
  userid         tickets.user_id%TYPE;
  supportstaffid tickets.support_staff_id%TYPE;
  resolution     tickets.resolution_details%TYPE;
BEGIN
  jobs_done(month_start, month_end, userid, supportstaffid, resolution);
END;
/

【问题讨论】:

【参考方案1】:

基于新信息:您的查询返回多条记录。使用这种类型的选择,您只能获取一行。尝试使用光标。例如结构如下:

declare
  cursor c1 is <select your thing from table>;
begin
 for r1 in c1
loop
  dbms_output.put_line (r1.firstcolumn|| r2.anothercolumn );
end loop;
end;

【讨论】:

我不认为这是我收到错误的问题:RA-01422:精确提取返回的行数超过了请求的行数 ORA-06512:在“SYSTEM.JOBS_DONE”,第 9 行 ORA-06512:在第 8 行 01422. 00000 - “精确提取返回的行数超过了请求的行数” *原因:精确提取中指定的数量小于返回的行数。 *操作:重写查询或更改请求的行数 正确。这意味着您的代码没有到达执行 dbms_output 的地步。【参考方案2】:

问题是它不会那样工作。如果您的目标是查看输出,则必须迭代结果列表。你写的对 PL/SQL 块没有意义。 您必须创建一个游标来保存结果并迭代到其中。

CREATE OR REPLACE PROCEDURE jobs_done(
month_start IN tickets.DATE_REPORTED%TYPE,
month_end IN tickets.DATE_RESOLVED%TYPE,
userid OUT TICKETS.USER_ID%type,
supportstaffid OUT tickets.support_staff_id%type,
resolution OUT tickets.resolution_details%type)
AS
Begin
  DECLARE
  CURSOR V_CURS IS
  select user_id, support_staff_id, resolution_details 
  where DATE_RESOLVED >= month_start AND DATE_RESOLVED <= month_end;
  BEGIN
      DBMS_OUTPUT.PUT_LINE('userid, supportstaffid, resolution');
      FOR V_CURS_RES IN V_CURS LOOP
       DBMS_OUTPUT.PUT_LINE(V_CURS_RES.userid || V_CURS_RES.supportstaffid || V_CURS_RES.resolution);   
      END LOOP;
  END;      


END jobs_done;

set serveroutput on
DECLARE
  month_start tickets.DATE_REPORTED%TYPE := &Enter_date_start;
  month_end tickets.DATE_RESOLVED%TYPE := &Enter_date_end;
  userid TICKETS.USER_ID%type;
  supportstaffid tickets.support_staff_id%type;
  resolution tickets.resolution_details%type;
BEGIN
  jobs_done(month_start, month_end, userid, supportstaffid, resolution);
End;

【讨论】:

【参考方案3】:
    Try this. Returning refcursor is the best way for this kind of resultset.

    CREATE OR REPLACE PROCEDURE jobs_done(
    month_start IN tickets.DATE_REPORTED%TYPE,
    month_end IN tickets.DATE_RESOLVED%TYPE,
    cur_out OUT sys_refcursor
    )
     AS
     OPEN cur_out FOR
     select user_id, support_staff_id, resolution_details 
     from tickets where DATE_RESOLVED >= month_start AND DATE_RESOLVED <= month_end;
     END jobs_done;

------------------------execute-----------------------------------------

var ls refcursor;
EXEC jobs_done(input1,input2,:ls);
print ls;

------------------------------------------------------------------------

【讨论】:

以上是关于PL/SQl sqldeveloper想在plsql中输出多行的主要内容,如果未能解决你的问题,请参考以下文章

连接PL/SQL

PLsq developer 只能连接ORACLE数据库么

PL/SQL 不打印到控制台(在 SQLDeveloper 中)

pl/sql使用技巧

PL/SQL Developer连接本地Oracle 11g 64位数据库

解决PL/SQL Developer过期