在运行以下程序时,我收到以下错误
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 ;
答案 0 :(得分:2)
您无法在标量变量中获取多行;例如:
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
如果您需要单个值,则必须确保查询仅返回一行:
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
答案 1 :(得分:0)
正如错误所示,并在下面说明您的选择查询返回的行数超过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;
答案 2 :(得分:0)
当select语句返回多行时,在Select Into语句中遇到错误。由于静态变量只能在其中存储一个值,我们需要做的是更改select语句的where子句以仅获取一个记录,或者在for循环中使用该查询。