I am trying to select some rows and put the data in a variable.the procedure is
CREATE OR REPLACE PROCEDURE GET_MESSAGE
AS
V_RESULT VARCHAR2(2000);
begin
EXECUTE IMMEDIATE 'SELECT MESSAGE_ID FROM MSG_TABLE WHERE ( DATE_OF_OPERATION BETWEEN 20180530 AND 20180622) AND (ROWNUM BETWEEN 1 and 2)' INTO V_RESULT;
dbms_output.put_line('V_RESULT:'||V_RESULT);
end;
/
When executing the procedure I am getting the following error
Error starting at line : 37 in command -
BEGIN GET_MESSAGE; END;
Error report -
ORA-01422: exact fetch returns more than requested number of rows
ORA-06512: at "PG_DBO11.GET_MESSAGE", line 8
ORA-06512: at line 1
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
the MSG_TABLE contains following columns and data
MESSAGE_ID DATE_OF_OPERATION MESSAGE
1000 20180530 AABC
1001 20180622 XXYZ
The query itself is working fine. I don't know how to store the selected rows into a variable. I may be missing a very general thing here.
ROWNUM BETWEEN 1 and 2also indicates a misunderstanding on howrownumworks. If you intend to do something likeROWNUM BETWEEN 3 and 6you will find out that that won't work. You need to use something likeoffset 1 rows fetch first 2 rows onlyinstead