I get error when I use this:
PROCEDURE GET_BY_CRIT(vchFilter varchar2(500),
intCantTotal OUT INT,
curResult OUT sys_refcursor)
IS
BEGIN
OPEN curResult FOR
'SELECT COLUMN1,COLUMN2 FROM SOME_TABLE WHERE '||vchFilter
EXECUTE IMMEDIATE 'SELECT COUNT(*) FROM SOME_TABLE WHERE '||vchFilter
INTO intCantTotal
END
Error:
ORA-00936: missed expression
But when I execute each sentence by separate it run correcly.
The error you’re getting doesn’t seem to make sense. Oracle should be throwing a compilation error because parameters to functions don’t have a length.
vchFiltershould be declared as aVARCHAR2, not aVARCHAR2(500).Additionally, as Lolo pointed out in the comments, statements in a PL/SQL block need to be terminated with semicolons.
Be aware as well that there is no guarantee that second SQL statement will see the same COUNT that the first SQL statement did unless you can guarantee that
SOME_TABLEis not being modified by any other sessions at the same time you’re querying it. I’d generally be rather wary of a need to run a query and execute a separate count– that generally indicates a more basic problem. If you need the COUNT to be consistent with the query you’re running, you’d want to add an analytic COUNT to your query and let the caller fetch that column.