how can i do if my select dont come with result using SYS_REFCURSOR
?
what i have try so far is using NO_DATA_FOUND
, but its not working, my STATUS
keep returning me as = 1
code:
...
MYVARIABLE IN OUT SYS_REFCURSOR
...
OPEN MYVARIABLE FOR
SELECT NAME FROM TABLE WHERE COD = 1;
STATUS := 1;
EXCEPTION
WHEN NO_DATA_FOUND THEN
STATUS := 0;
any ideias ?
thanks!
the real sql is something like this:
Edit
V_SQL := 'SELECT SUM(T1.VLRLIQ) VALOR,T1.CODCLI,T1.NOMCLI
,(SELECT METCLI FROM WEB_CRM_CLIVEN T2
WHERE T2.CODCLI = T1.CODCLI AND T2.CODVEN = '|| P_CODVEN ||'
AND T2.MES = '|| V_MES ||' AND T2.ANO = '|| V_ANO ||') META
FROM SAPIENS.USU_VRESNFV T1,SAPIENS.E085CLI T2
WHERE T1.CODVEN = '|| P_CODVEN ||'
AND TO_CHAR(T1.DATEMI,''YYYY'') = '|| V_ANO ||'
AND TO_CHAR(T1.DATEMI,''MM'') = '|| V_MES ||'
AND T1.VENFAT = ''S''
'|| V_CGCCPF ||'
'|| V_NOMCLI ||'
AND T2.CODCLI = T1.CODCLI
AND T1.CODEMP = 1
GROUP BY T1.CODCLI,T1.NOMCLI
UNION
SELECT SUM(''0'') VALOR,CODCLI,NOMCLI,(SELECT METCLI FROM WEB_CRM_CLIVEN T3
WHERE T3.CODCLI = T2.CODCLI AND T3.CODVEN = '|| P_CODVEN ||'
AND T3.MES = '|| V_MES ||' AND T3.ANO = '|| V_ANO ||') META
FROM SAPIENS.E085CLI T2
WHERE
CODCLI IN (SELECT CODCLI FROM WEB_CRM_VEN_CARTEIRA
WHERE CODVEN = '|| P_CODVEN ||' AND MES = '|| V_MES ||' AND ANO = '|| V_ANO ||')
'|| V_CGCCPF ||'
'|| V_NOMCLI ||'
GROUP BY CODCLI,NOMCLI
ORDER BY VALOR DESC';
STATUS := 1;
OPEN RESULTADO FOR V_SQL;
In your code, you are just opening the cursor but not fetching from it. When you open a cursor, PL/SQL executes the query for that cursor. It also identifies the rows that meet the criteria in the WHERE clause and join conditions. The OPEN does not actually retrieve any of these rows; that action is performed by the FETCH statement. You would then use cursor attributes to check if the result set is empty; if it is, then the following cursor attributes would have these values: %FOUND = FALSE, %NOTFOUND = TRUE, and %ROWCOUNT = 0.
Here is an example:
To slightly modify @Eddie Awad's answer, the usual code pattern I use for fetching from a cursor variable is as follows:
The idea is to open the cursor variable (or get it back from a procedure), then loop until all rows have been fetched from the cursor.
Share and enjoy.