0

I am using the following query to return a single value:

select er.elig_code
from idm_elig_rule er 
where ER.ROLE_CODE = role_code and ER.SERVICE_CODE = service_code; 

When I substitute values for the variables and run it as a single SQL statement it will return just a single value like I want it too. However, when I place the statement in a cursor and loop through the results, it returns all rows in the table. I've also tried a "select into" statement but that still returns all 85 rows in the table. I have a unique index in the idm_elig_rule table that is a combination of the role_code, service_code and another column.

EDIT: Here's how I'm testing it out - using an anonymous block:

declare
    role_code_in IDM_ELIG_RULE.ROLE_CODE%type := 'CEMP';
    service_code_in IDM_ELIG_RULE.SERVICE_CODE%type := 'PORTL'; 
    cursor get_elig_code is 
        select ER.ELIG_CODE
        from idm_elig_rule er 
        where ER.ROLE_CODE = role_code_in and ER.SERVICE_CODE = service_code_in; 
begin 
    for r in get_elig_code 
    loop
        DBMS_OUTPUT.PUT_LINE(r.elig_code);
    end loop;  
end;

EDIT: I changed the names of the variables in the where clause and that resolved the issue.

3
  • 1
    Are you sure that the parameters used in the cursor (role_code and service_code) have the right values at the time of execution? (E.g. can you print out the values using dbms_output or verify them in any other way?) Commented Feb 7, 2012 at 14:19
  • 1
    show how you are looping through results
    – tbone
    Commented Feb 7, 2012 at 14:21
  • Boom! just added my answer and the other two came in at the same time. I think the fact they all say the same thing should tell you your answer!
    – Ollie
    Commented Feb 7, 2012 at 14:25

3 Answers 3

9

You have a Name Resolution issue with your variable names since the bind variable names are the same as the column names. Change your variable names to avoid the PL/SQL compiler from deciding for you.

Oracle PL/SQL Variable Name Resolution

1
  • This one could be really frustrating to find! Commented Sep 20, 2013 at 1:09
3

Since your variable names are the same as the column names, it is possible that the parser is seeing them as the column names, not as variable, and therefore you are getting all the rows where the columns are equal to themselves, which would only exclude rows with NULL values.

I'd suggest always using different names for your variables than for the columns, to avoid any confusion. A common technique is to use some prefix on the variable name, such as l_ for local variable, p_ for parameters, or g_ for globals.

In this case, it may be better to parameterize the cursor:

CURSOR code_cur (p_role_code INTEGER, p_service_code INTEGER) IS
select er.elig_code
from idm_elig_rule er 
where ER.ROLE_CODE = p_role_code and ER.SERVICE_CODE = p_service_code;

Then you would pass values when opening the cursor, e.g.:

FOR code_rec IN code_cur ( my_role_code_value, my_service_code_value ) LOOP
3

From the limited information in your question i'd say that your parameters are not being instantiated correctly and your select is resolving to:

select er.elig_code
  from idm_elig_rule er
 where ER.ROLE_CODE = role_code 
   and ER.SERVICE_CODE = service_code; 

Where the DB is seeing role_code and service_code as the columns and not variables so it will always return all rows from your table.

Change the names of your variables to something different from your column names to see if this is the case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.