ORA-06512: 精确提取 returns 多于请求的行数

ORA-06512:exact fetch returns more than requested number of rows

虽然运行下面的程序我得到下面的错误

Error report:
ORA-01422: exact fetch returns more than requested number of rows
ORA-06512: at "my_db.CREATE_CTAB_FILE", line 51
ORA-06512: at line 2
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

你能建议我如何将获取的数据分配给变量吗? 我怀疑数据类型为 varchar2 的变量 (lnbuf) 不接受 select 语句获取的行。请帮我解决这个错误

create or replace 
PROCEDURE CREATE_CTAB_FILE (f_ctab_id IN T_WN_CTAB_str.wn_ctab_id%type,
                                P_SYN_DIR in varchar2,
                                p_user    IN varchar2)

is 
FILEHANDLER WN_DB_UTL_FILE.FILE_TYPE;
LNBUF varchar2(4000) := null;
--cast(lnbuf as char(4000));
--LNBUF clob;

v_file     varchar2(256);
V_DATA_LOG varchar2(256);
V_WN_COUNTRY T_WN_CTAB_STR.WN_COUNTRY%type;
V_H_L varchar2(256);


BEGIN


for CTAB_REC in 
(select distinct WN_COUNTRY, WN_PRHB_DATE
from t_wn_ctab_h 
where wn_ctab_id =f_ctab_id)

loop

v_wn_country:=ctab_rec.wn_country;

--bkk_solution.wn_insert_res_log('create_ctab', NULL, NULL, help_rec.wn_cty,    help_rec.wn_vers_sol, 'START', p_cty);

v_file:=v_wn_country||'_CTAB'||'.txt';

fileHandler := WN_DB_UTL_FILE.FOPEN(P_SYN_DIR,V_FILE,'W');
--p_file_id := fileHandler.id;

lnbuf := ('#' || rpad(' ',42,' ') ||'V3.4.5'||rpad(' ',197,' ') ||            '000000000000000000000000000'||to_char(sysdate,'YYYYMMDD')||     to_char(ctab_rec.WN_PRHB_DATE,'YYYYMMDD') ||rpad(' ',48,' ')|| '1');   -- '<html>' -Tag ausgeben
WN_DB_UTL_FILE.PUTF(fileHandler, lnbuf);
 Loop
  LNBUF   := NULL ;

   SELECT (   CAST ('LT01' AS CHAR (8))
       || CAST (REC.WN_COUNTRY AS CHAR (3))
       || CAST (rec.WN_NORMSSNR_WW AS CHAR (32))
       || CAST (REC.WN_SOFTWARE AS CHAR (7))
       || CAST (REC.WN_NORMSSNR AS CHAR (32))
       || RPAD (' ', 32, ' ')
       || CAST (REC.WN_NAME_WW AS CHAR (16))
       || CAST (REC.WN_NAME AS CHAR (16))
       || CAST (REC.WN_NORMBYTE AS CHAR (6))
       || CAST ('0' AS CHAR (3))
       || CAST ('ST' AS CHAR (2))
       || RPAD (' ', 70, ' ')
       || CAST ('ST' AS CHAR (3))
       || RPAD (' ', 60, ' ')
       || TO_CHAR (rec.WN_FOD, 'YYYYMMDD')
       || TO_CHAR (rec.WN_LOD, 'YYYYMMDD'))
 INTO lnbuf
 FROM t_wn_ctab_str rec
WHERE rec.wn_ctab_id = 1234;
WN_DB_UTL_FILE.PUTF(fileHandler, lnbuf); 
end LOOP;
WN_DB_UTL_FILE.FCLOSE(fileHandler);
end LOOP; 
END CREATE_CTAB_FILE ;

您不能将多行数据提取到一个标量变量中;例如:

SQL> declare
  2      vName varchar2(100);
  3  begin
  4      select first_name
  5      into vName
  6      from employees
  7      where rownum < 3;
  8      --
  9      dbms_output.put_line(vName);
 10  end;
 11  /
declare
*
ERROR at line 1:
ORA-01422: exact fetch returns more than requested number of rows
ORA-06512: at line 4

如果您需要单个值,则必须确保您的查询仅 returns 一行:

SQL> declare
  2      vName varchar2(100);
  3  begin
  4      select first_name
  5      into vName
  6      from employees
  7      where rownum = 1;
  8      --
  9      dbms_output.put_line(vName);
 10  end;
 11  /
Ellen

如果需要获取多行,可以使用 BULK COLLECT 和适当的变量:

SQL> declare
  2      type   tTabNames is table of varchar2(100);
  3      vNames tTabNames ;
  4  begin
  5      select first_name
  6      bulk collect into vNames
  7      from employees
  8      where rownum < 3;
  9      --
 10      for i in 1 .. vNames.count loop
 11          dbms_output.put_line(vNames(i));
 12      end loop;
 13  end;
 14  /
Ellen
Sundar

正如错误所建议的那样,并在下面解释说您的 select 查询返回的行数超过 1 行,而您正试图将其存储在一个变量中,这引起了问题。如下更改循环:

for rec in (
               SELECT (   CAST ('LT01' AS CHAR (8))
                   || CAST (REC.WN_COUNTRY AS CHAR (3))
                   || CAST (rec.WN_NORMSSNR_WW AS CHAR (32))
                   || CAST (REC.WN_SOFTWARE AS CHAR (7))
                   || CAST (REC.WN_NORMSSNR AS CHAR (32))
                   || RPAD (' ', 32, ' ')
                   || CAST (REC.WN_NAME_WW AS CHAR (16))
                   || CAST (REC.WN_NAME AS CHAR (16))
                   || CAST (REC.WN_NORMBYTE AS CHAR (6))
                   || CAST ('0' AS CHAR (3))
                   || CAST ('ST' AS CHAR (2))
                   || RPAD (' ', 70, ' ')
                   || CAST ('ST' AS CHAR (3))
                   || RPAD (' ', 60, ' ')
                   || TO_CHAR (rec.WN_FOD, 'YYYYMMDD')
                   || TO_CHAR (rec.WN_LOD, 'YYYYMMDD'))  col1           
             FROM t_wn_ctab_str rec
            WHERE rec.wn_ctab_id = 1234 )
loop

LNBUF   := NULL ;

LNBUF := rec.col1;   

 WN_DB_UTL_FILE.PUTF(fileHandler, lnbuf); 

end LOOP;

当select 语句returns 多于一行时,在Select Into 语句中遇到错误。由于静态变量只能在其中存储一个值,我们需要做的是将 select 语句的 where 子句更改为仅获取一条记录,或者在 for 循环中使用查询。