Why is my Oracle PROCEDURE returning 0 records

时间:2015-09-01 22:33:14

标签: oracle oracle-sqldeveloper

If I run this static SQL in ORACLE SQL DEVELOPER:

SELECT appl_id
     FROM grant_appls where full_appl_num IN( 
 '1R01HL129077-01','2R01HL075494-10A1','2P01HL062426-16') AND SUBPROJECT_ID is not null;

I get these results:

APPL_ID
8855105
8855112
8855104
8855108
8855109
8855107
8855106

Now I write the PROCUDERE and put the Static SQL in there:

create or replace PROCEDURE GET_APPLIDS_BY_FULL_GRANT_NUM (
   fullGrantNumList IN VARCHAR2,
applIdRecordSet OUT SYS_REFCURSOR)
AS
BEGIN
 OPEN applIdRecordSet FOR
 SELECT appl_id
 FROM grant_appls where full_appl_num IN
 (
     '1R01HL129077-01','2R01HL075494-10A1','2P01HL062426-16'
 ) AND SUBPROJECT_ID is not null;
END GET_APPLIDS_BY_FULL_GRANT_NUM;

I could have sworn at one point I was getting results since I have the static comma delimeted list. But now I can't even get results with this.

The Ouput Variables window has the variable APPLIDRECORDSET but there are no values for APPL_ID.

The final version should look something like this:

create or replace PROCEDURE GET_APPLIDS_BY_FULL_GRANT_NUM (
   fullGrantNumList IN VARCHAR2,
   applIdRecordSet OUT SYS_REFCURSOR)
AS
BEGIN
 OPEN applIdRecordSet FOR
     SELECT appl_id
     FROM grant_appls where full_appl_num IN
     (
     fullGrantNumList
 ) AND SUBPROJECT_ID is not null;
END GET_APPLIDS_BY_FULL_GRANT_NUM;

So of course when I run this I am getting back null:

VARIABLE cur REFCURSOR
EXECUTE GET_APPLIDS_BY_FULL_GRANT_NUM("'1R01HL129077-01','2R01HL075494-      10A1','2P01HL062426-16'",:cur);
SELECT :cur FROM dual;

1 个答案:

答案 0 :(得分:1)

Your parameter fullGrantNumList won't be used by Oracle the way you think. Oracle takes the bind variables and treats it as one value, it doesn't do a text replace like you think. Here is what is actually happening to your query:

select appl_id from grant_appls where full_appl_num in ('''1R01HL129077-01'',''2R01HL075494-      10A1'',''2P01HL062426-16''') amd subproject_id is null;

This is actually one of the nice things about bind variables is that they protect you from SQL Injection attacks.

My recommendation would be to either pass in a list of values as a table type or convert the statement to a string and use dynamic SQL to execute it.

Dynamic SQL