Results 1 to 11 of 11
  1. #1
    Join Date
    Mar 2007
    Posts
    5

    Unanswered: If statement in a cursor

    Hi

    I am trying to have a if statement inside a cursor clause. But it gives me error saying that "PLS-00103: Encountered the symbol "IF" when expecting one of the following: ( select <a SQL statement>"


    Code:
    CREATE OR REPLACE PROCEDURE SP_SYNOPTEX_GENERATE_REPORT(
    	typeID IN NUMBER
    ) IS
    
    data varchar2(16);
    dataresult varchar2(16);
     
    cursor c1 is
            IF typeID=1 then 
    	select value from TableName#1
            ELSE
    	select value from TableName#2
            END IF;
    	where id='1';
    
    begin
    
    	open c1;
    	dataresult:='';
    	 loop
    	 fetch c1 into dataresult;
    	 EXIT WHEN c1%NOTFOUND;
             data:=concat(to_char(data), to_char(dataresult));
    	 end loop;
    	close c1;
    end
    Thanks for your help

  2. #2
    Join Date
    Aug 2003
    Location
    Where the Surf Meets the Turf @Del Mar, CA
    Posts
    7,776
    Provided Answers: 1
    >I am trying to have a if statement inside a cursor clause
    Well, that is the problem.
    What you have is invalid syntax.
    Remove the IF construct from the cursor.
    You can lead some folks to knowledge, but you can not make them think.
    The average person thinks he's above average!
    For most folks, they don't know, what they don't know.
    Good judgement comes from experience. Experience comes from bad judgement.

  3. #3
    Join Date
    Mar 2007
    Posts
    5
    I need the if clause because that will tell me what table to select the values from. if the type id is 1 then go to table#1 if the type id is 2 then to table #2 etc.
    Is there any other way to specify this by using cursors.

    Thanks

  4. #4
    Join Date
    Nov 2006
    Location
    Indianapolis
    Posts
    90
    (cursors) = sql
    (if then else) = PL/SQL

    2 different languages

  5. #5
    Join Date
    Jan 2004
    Location
    Croatia, Europe
    Posts
    4,094
    Provided Answers: 4
    It seems that you'll need to declare two cursors and, depending on procedure parameter value, open and work with the one that suits your needs. Or, you might dynamically create statement(s), but this idea isn't particularly good.

  6. #6
    Join Date
    Mar 2007
    Posts
    5
    Thanks. I used dynamic cursors and it works now.

  7. #7
    Join Date
    Jun 2004
    Location
    Liverpool, NY USA
    Posts
    2,509
    Code:
    CREATE OR REPLACE PROCEDURE SP_SYNOPTEX_GENERATE_REPORT(
        typeID IN NUMBER
    ) IS
    
    data varchar2(16);
     
    cursor c1 is
         select value from TableName#1
            where typeid = 1
            union all
        select value from TableName#2
            where typeid = 2
            and id='1';
    
    begin
    
        for pnt in c1 loop
           data := data||pnt.value;
        end loop;
    end
    Last edited by beilstwh; 03-02-07 at 18:03.
    Bill
    You do not need a parachute to skydive. You only need a parachute to skydive twice.

  8. #8
    Join Date
    Jan 2004
    Location
    Croatia, Europe
    Posts
    4,094
    Provided Answers: 4
    If my new BrainCompiler 0.99b isn't wrong again, it seems that result of your cursor, Bill, will be records from both 'TableName#1' and 'TableName#2'. As I understood OP's question, he'd like to select records from only one of those tables, not both.

    But, it can be fixed. What about such improvement of your cursor? If we assume that possible values of IN parameter 'par_typeID' may be 1 (so that we'd like to select from 'TableName#1') or 2 (select from 'TableName#2'), perhaps something like this might give OP another idea:
    Code:
    CREATE OR REPLACE PROCEDURE SP_SYNOPTEX_GENERATE_REPORT
      (par_typeID IN NUMBER)
    IS
      cursor c1 is
        select x.value
        from (select '1' ID, value from TableName#1
                where id = '1'
              union all
              select '2' ID, value from TableName#2
                where id = '1'
             ) x
        where x.id = par_typeID;
    
    ...

  9. #9
    Join Date
    May 2004
    Location
    Dominican Republic
    Posts
    721
    Code:
    select value
      from tablename#1
     where 1 = decode( typeID, 1, 1, 0 )
       and id = '1'
    union all
    select value
      from tablename#2
     where 1 = decode( typeid, 1, 0, 1 )
       and id = '1'

  10. #10
    Join Date
    Mar 2007
    Posts
    1
    hi,
    use the ref cursors for this .

    CREATE OR REPLACE PROCEDURE SP_SYNOPTEX_GENERATE_REPORT(
    typeID IN NUMBER
    ) IS

    data varchar2(16);
    dataresult varchar2(16);
    QRYSTR VARCHAR2(400);
    TYPE REF_CUR IS REF CUSOR;
    C1 REF_CUR;

    begin
    IF typeID=1 then
    QRYSTR:='select value from TableName#1';
    ELSE
    QRYSTR:='select value from TableName#2';
    END IF;
    QRYSTR:=QRYSTR||' where id=''1'';

    open c1 FOR QRYSTR;
    dataresult:='';
    loop
    fetch c1 into dataresult;
    EXIT WHEN c1%NOTFOUND;
    data:=concat(to_char(data), to_char(dataresult));
    end loop;
    close c1;
    end;

  11. #11
    Join Date
    Jun 2004
    Location
    Liverpool, NY USA
    Posts
    2,509
    If the IN parameter typeid is set to 1 ONLY the first query in the union all will be returned. If they pass 2, then ONLy the second query will be returned. If they pass anything else, nothing will be returned.
    Bill
    You do not need a parachute to skydive. You only need a parachute to skydive twice.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •