Where clause in cursor

I often have a problem with cursors and after playing around they usually work. The problem is I don't really know what I'm doing as I'm relativly new to all this! Perhaps someone can help ;)
I have the folling code to test my cursor:
declare
vcrn varchar2(8) := :module_choice.crn;
vgroup_code varchar2(10) := :module_choice.group_code;
vclass_type varchar2(10) := :module_choice.class_type;
i int := 0;
cursor att_cur is
select student_id, attended, notes
from attendance_register
where crn = vcrn ;
AND group_code = vgroup_code
AND class_type = vclass_type;
BEGIN
for row in att_cur loop
     i := i + 1;
end loop;
message(i);
END;
the message shows i = 0, but I know it's not. When I comment out the where clause it works.
I guess my real question would be how do you get a where clause to work like this??
Thanks in Advance
Mark

This where clause will succeed if some of the variables are null:
where ( crn = vcrn or vcrn is null )
  AND (group_code = vgroup_code or vgroup_code is null)
  AND (class_type = vclass_type or vclass_type is null)But I am very concerned about your Char datatypes. Those trailing spaces will give you nothing but lots of grief. Unless you have a compelling reason to keep them, they should be changed to varchar2.

Similar Messages

  • Passing where clause of cursor

    my requirement is that i want to declare a cursor but the problem is that where clause of sql statement of cursor is dynamically generated according to user clicks........ e. g. number of conditions in where clause may change.....
    so..... how can i declare the cursor...how do i pass where clause to any procedure if i write procedure or function......

    u can use for-loop cursors like this,
    for v_rec in 'select * from emp where' || ur_conditions ||'
    see oracle documentation for further help and examples

  • Dynamic where clause for cursor

    Hi Friends,
    I have a stored procedure with a single input parameter where in the input comes as
    a string of values (ex: EMPLOYEE NAMES) separated by commas. I use this parameter in my cursor query
    and my requirement is like when the parameter is NULL the query has to run for ALL the employees but if
    its not, it has to run for the EMPLOYEE NAMES passed as the input parameter.
    What are the possible and apt solutions for this requirement ?
    Should I go for dynamic sql or do I have any other optimal solutions?
    Please advice me.
    Thanks
    Zara

    Dear,
    I have a stored procedure with a single input parameter where in the input comes as
    a string of values (ex: *EMPLOYEE NAMES*) separated by commas. I use this parameter in my cursor query
    and my requirement is like when the parameter is NULL the query has to run for ALL the employees but if
    its not, it has to run for the EMPLOYEE NAMES passed as the input parameter.
    What are the possible and apt solutions for this requirement ?
    Should I go for dynamic sql or do I have any other optimal solutions?
    Please advice me.I would suggest you to let down this dynamic SQL and those concatenations that are going to introduce a threat of SQL injection and non shared code into your application.
    And, as far as you've opted for a stored procedure I want to suggest you to use
    (a) stored procedure in wich only static SQL is used
    (b) if point (a) is respected then you don't have to care about using litteral inside your stored procedure. All what you will do in this procedure will be 'auto-binded'; that is PL/SQL (static).
    (c) However, make sure to call your stored procedure using bind variables; otherwise your shared pool (v$sql) will be full of hundred and thousands of calls to your stored procedure.
    Best Regards
    Mohamed Houri

  • Passing where and group by clause to cursor

    I am working on a procedure that builds a where clause and needs a group by clause to return the correct results. I am trying to pass both the where and group by variables into the cursor.
    The variables are getting populated correctly, but when the cursor gets created, the variables are not in the cursor.
    Here is the code I'm working with. It is a part of a package, but makes no calls to other parts of the package.
    PROCEDURE createFollowUpTask_Exposure( psUppgkedjetyp IN tis.tial.uppgkedjetyp%TYPE default NULL,
    psAlarmtyp IN tis.tial.alarmtyp%TYPE default NULL,
    psSubtyp IN tis.tial.subtyp%TYPE default NULL,
    pnDays IN NUMBER default NULL,
    psKampkod IN tis.tiin.kampkod%TYPE default NULL,
    psKatnr IN tis.tiin.katnr%TYPE default NULL,
    psUtgava IN tis.tiin.utgava%TYPE default NULL,
    psKatslag IN tis.tikg.katslag%TYPE default NULL,
    psProdsyst IN tis.tikg.prodsyst%TYPE default NULL,
    psUppgtyp IN tis.tiin.uppgtyp%TYPE default NULL,
    psProdkod IN tis.tiin.prodkod%TYPE default NULL,
    psStatus IN tis.tiin.status%TYPE default NULL
    ) AS
    cTIAL tis.tial%ROWTYPE;
    vLopnr tis.tial.lopnr%TYPE;
    vSqlWhere VARCHAR2(4000);
    vGroupBy VARCHAR2(1000) := ' tiin.kampkod, tiin.abnr, tiko.fordsalj';
    cSelectCursor SYS_REFCURSOR;
    vSqlSelect VARCHAR2(4000);
    psDays VARCHAR2(50);
    cRec T_TIAL_REC;
    nCount number := 0;
    CURSOR cSqlSelect( SqlWhere IN VARCHAR2, GroupBy IN VARCHAR2) IS
    SELECT tiin.kampkod, tiin.abnr, tiko.fordsalj, MAX(tici.regdat) ALARMDATE
    FROM tis.tiin
    JOIN tis.tiko ON tiin.kampkod = tiko.kampkod AND tiin.abnr = tiko.abnr
    JOIN core.tici ON tiin.kampkod = tici.kampkod AND tiin.abnr = tici.abnr AND tici.inplnr = tiin.inplnr
    WHERE 1=1 || SqlWhere
    GROUP BY GroupBy;
    BEGIN
    -- If these parameters are null, raise error
    IF psUppgkedjetyp IS NULL and psSubtyp IS NULL THEN
    raise_application_error(-20001,
    'Either Event Chain or Starting Event must be assigned');
    END IF;
    -- Populate TIAL values
    IF psUppgkedjetyp IS NOT NULL THEN
    cTIAL.Uppgkedjetyp := psUppgkedjetyp;
    END IF;
    IF psAlarmtyp IS NOT NULL THEN
    cTIAL.Alarmtyp := psAlarmtyp;
    END IF;
    cTIAL.Handklass := 'T';
    cTIAL.Blobid := 0;
    IF pnDays IS NOT NULL THEN
    psDays := '+ '||pnDays;
    END IF;
    IF psSubtyp IS NOT NULL THEN
    cTIAL.Subtyp := psSubtyp;
    END IF;
    -- Create Where clause for cursor
    vSqlWhere := '';
    IF psKampkod IS NOT NULL THEN
    vSqlWhere := vSqlWhere||' AND tiin.kampkod = '''|| psKampkod||'''';
    END IF;
    IF psKatnr IS NOT NULL THEN
    vSqlWhere := vSqlWhere||' AND tiin.katnr = '''||psKatnr||'''';
    END IF;
    IF psUtgava IS NOT NULL THEN
    vSqlWhere := vSqlWhere||' AND tiin.utgava = '''||psUtgava||'''' ;
    END IF;
    IF psKatslag IS NOT NULL THEN
    vSqlWhere := vSqlWhere||' AND tikg.katslag = '''||psKatslag||'''';
    END IF;
    IF psProdsyst IS NOT NULL THEN
    vSqlWhere := vSqlWhere||' AND tikg.prodsyst = '''||psProdsyst||'''';
    END IF;
    IF psUppgtyp IS NOT NULL THEN
    vSqlWhere := vSqlWhere||' AND tiin.uppgtyp = '''||psUppgtyp||'''';
    END IF;
    IF psProdkod IS NOT NULL THEN
    vSqlWhere := vSqlWhere||' AND tiin.prodkod = '''||psProdkod||'''';
    END IF;
    IF psStatus IS NOT NULL THEN
    vSqlWhere := vSqlWhere||' AND tiin.status = '''||psStatus||'''';
    END IF;
    -- Loop through all records meeting input parameters and set required TIAL values.
    FOR i IN cSqlSelect(vSqlWhere, vGroupBy)
    LOOP
    --FETCH cSelectCursor INTO cRec;             
    cTIAL.Kampkod := '';
    cTIAL.Abnr := '';
    cTIAL.Sign := '';
    cTIAL.Alarmdate := '';
    cTIAL.Kampkod := i.Kampkod;
    cTIAL.Abnr := i.Abnr;
    cTIAL.Sign := i.fordsalj;
    cTIAL.Alarmdate := i.alarmdate;
    nCount := nCount + 1;
    IF vLopnr = -1 THEN
    raise_application_error(-20002,
    'Error Creating task for: '||cTIAL.Kampkod||' '||cTIAL.Abnr||' Sales Rep: '||cTIAL.Alarmdate);
    END IF;
    END LOOP;
    DBMS_OUTPUT.PUT_LINE('I created '||nCount||' records.');
    END createFollowUpTask_Exposure;
    Thanks in advance for any help.

    Hi,
    Welcome to the forum!
    Try this (not tested) as an example:
    PROCEDURE createFollowUpTask_Exposure(psUppgkedjetyp IN tis.tial.uppgkedjetyp%TYPE DEFAULT NULL,
                                          psAlarmtyp     IN tis.tial.alarmtyp%TYPE DEFAULT NULL,
                                          psSubtyp       IN tis.tial.subtyp%TYPE DEFAULT NULL,
                                          pnDays         IN NUMBER DEFAULT NULL,
                                          psKampkod      IN tis.tiin.kampkod%TYPE DEFAULT NULL,
                                          psKatnr        IN tis.tiin.katnr%TYPE DEFAULT NULL,
                                          psUtgava       IN tis.tiin.utgava%TYPE DEFAULT NULL,
                                          psKatslag      IN tis.tikg.katslag%TYPE DEFAULT NULL,
                                          psProdsyst     IN tis.tikg.prodsyst%TYPE DEFAULT NULL,
                                          psUppgtyp      IN tis.tiin.uppgtyp%TYPE DEFAULT NULL,
                                          psProdkod      IN tis.tiin.prodkod%TYPE DEFAULT NULL,
                                          psStatus       IN tis.tiin.status%TYPE DEFAULT NULL) AS
       cTIAL         tis.tial%ROWTYPE;
       vLopnr        tis.tial.lopnr%TYPE;
       vSqlWhere     VARCHAR2(4000);
       vGroupBy      VARCHAR2(1000) := ' tiin.kampkod, tiin.abnr, tiko.fordsalj';
       cSelectCursor SYS_REFCURSOR;
       vSqlSelect    VARCHAR2(4000);
       psDays        VARCHAR2(50);
       cRec          T_TIAL_REC;
       nCount        NUMBER := 0;
       FUNCTION fnc_cSqlSelect(SqlWhere IN VARCHAR2,
                               GroupBy  IN VARCHAR2) RETURN VARCHAR2 IS
       BEGIN
          RETURN 'SELECT tiin.kampkod,
                 tiin.abnr,
                 tiko.fordsalj,
                 MAX(tici.regdat) ALARMDATE
            FROM tis.tiin
            JOIN tis.tiko ON tiin.kampkod = tiko.kampkod
                         AND tiin.abnr = tiko.abnr
            JOIN core.tici ON tiin.kampkod = tici.kampkod
                          AND tiin.abnr = tici.abnr
                          AND tici.inplnr = tiin.inplnr
           WHERE 1 = 1 ' || SqlWhere || ' GROUP BY ' || GroupBy;
       END fnc_cSqlSelect;
    BEGIN
       -- If these parameters are null, raise error
       IF psUppgkedjetyp IS NULL AND psSubtyp IS NULL THEN
          raise_application_error(-20001,
                                  'Either Event Chain or Starting Event must be assigned');
       END IF;
       -- Populate TIAL values
       IF psUppgkedjetyp IS NOT NULL THEN
          cTIAL.Uppgkedjetyp := psUppgkedjetyp;
       END IF;
       IF psAlarmtyp IS NOT NULL THEN
          cTIAL.Alarmtyp := psAlarmtyp;
       END IF;
       cTIAL.Handklass := 'T';
       cTIAL.Blobid    := 0;
       IF pnDays IS NOT NULL THEN
          psDays := '+ ' || pnDays;
       END IF;
       IF psSubtyp IS NOT NULL THEN
          cTIAL.Subtyp := psSubtyp;
       END IF;
       -- Create Where clause for cursor
       vSqlWhere := '';
       IF psKampkod IS NOT NULL THEN
          vSqlWhere := vSqlWhere || ' AND tiin.kampkod = ''' || psKampkod || '''';
       END IF;
       IF psKatnr IS NOT NULL THEN
          vSqlWhere := vSqlWhere || ' AND tiin.katnr = ''' || psKatnr || '''';
       END IF;
       IF psUtgava IS NOT NULL THEN
          vSqlWhere := vSqlWhere || ' AND tiin.utgava = ''' || psUtgava || '''';
       END IF;
       IF psKatslag IS NOT NULL THEN
          vSqlWhere := vSqlWhere || ' AND tikg.katslag = ''' || psKatslag || '''';
       END IF;
       IF psProdsyst IS NOT NULL THEN
          vSqlWhere := vSqlWhere || ' AND tikg.prodsyst = ''' || psProdsyst || '''';
       END IF;
       IF psUppgtyp IS NOT NULL THEN
          vSqlWhere := vSqlWhere || ' AND tiin.uppgtyp = ''' || psUppgtyp || '''';
       END IF;
       IF psProdkod IS NOT NULL THEN
          vSqlWhere := vSqlWhere || ' AND tiin.prodkod = ''' || psProdkod || '''';
       END IF;
       IF psStatus IS NOT NULL THEN
          vSqlWhere := vSqlWhere || ' AND tiin.status = ''' || psStatus || '''';
       END IF;ç
       -- Loop through all records meeting input parameters and set required TIAL values.
       OPEN cSelectCursor FOR fnc_cSqlSelect(vSqlWhere,
                                             vGroupBy);
       LOOP
          FETCH cSelectCursor
             INTO v; -- You must define a variable 'v' to hold the data of cursor
          EXIT WHEN cSelectCursor%NOTFOUND;
          --FETCH cSelectCursor INTO cRec;
          cTIAL.Kampkod   := '';
          cTIAL.Abnr      := '';
          cTIAL.Sign      := '';
          cTIAL.Alarmdate := '';
          cTIAL.Kampkod   := i.Kampkod;
          cTIAL.Abnr      := i.Abnr;
          cTIAL.Sign      := i.fordsalj;
          cTIAL.Alarmdate := i.alarmdate;
          nCount := nCount + 1;
          IF vLopnr = -1 THEN
             raise_application_error(-20002,
                                     'Error Creating task for: ' || cTIAL.Kampkod || ' ' ||
                                     cTIAL.Abnr || ' Sales Rep: ' || cTIAL.Alarmdate);
          END IF;
       END LOOP;
       CLOSE cSelectCursor;
       DBMS_OUTPUT.PUT_LINE('I created ' || nCount || ' records.');
    END createFollowUpTask_Exposure;
    /Regards,

  • Ref cursor argument in where clause

    Env: ORCL 9.2
    I have a func that uses the parameters in a where clause and returns a ref cursor as result. That works fine.
    I want to create an overloaded func that replaces one argument with a ref cursor. (instead of accepting a single value as an argument I want to accept multiple values) Can you specify the ref cursor in a where clause with out looping through the cursor ?
    CURRENT
    func(arg1,arg2,arg3) returns ref cursor
    is
    select blah from sometable s
    where s.a = arg1
    and s.b = arg2
    and s.c = arg3
    NEW
    func(ref_cur_arg1,arg2,arg3) returns ref cursor
    is
    select blah from sometable s
    where s.a = ref_cur_arg1
    and s.b = arg2
    and s.c = arg3
    is there something like:
    where s.a in (loop fetch ref_cur_arg1 end loop)
    thx

    Thanks Rich,
    That's pretty much what I came up with:
    FUNCTION f_bond_price_w_bb_stat (
                                  p_id_ref gtyp_instr_id_ref,
                                  p_price_srce bond_price.PRICE_SRCE%type,
                                  p_price_type bond_price.PRICE_TYPE%type,
                                  p_price_date bond_price.PRICE_DATE%type)
    RETURN gtyp_bondprice_w_bb_stat_rfc
    IS
    lv_bondprice_rfc gtyp_bondprice_w_bb_stat_rfc;
    TYPE ARRAY1 IS TABLE OF instr_ext_id_map.ext_id_value%TYPE INDEX BY BINARY_INTEGER;
    t_instr_id ARRAY1;
    instr_ids INSTR_EXT_ID_T := INSTR_EXT_ID_T();
    BEGIN
         --suck the contents of the ref cursor into a local virtual tmp table
    FETCH p_id_ref BULK COLLECT INTO t_instr_id;
    FOR i IN 1..t_instr_id.COUNT LOOP
         instr_ids.extend;
              instr_ids(instr_ids.count) := t_instr_id(i);
    END LOOP;
    CLOSE p_id_ref;
    OPEN lv_bondprice_rfc FOR
    SELECT
    bs.ID_ISIN,
    bs.TICKER,
    bs.CPN,
    bs.MATURITY,
    round(months_between(bs.MATURITY,sysdate)/12,1),
    bs.ISSUER_INDUSTRY,
    bs.INDUSTRY_SECTOR,
    FROM bond_price b,
    instr_ext_id_map ext,
    etl.mdy_ratingstatic mrs,
         etl.mdy_extid mxid,
         etl.bloomberg_static bs
    WHERE b.INSTR_ID = ext.instr_id
    AND bs.ID_ISIN(+) = ext.ext_id_value
    AND bs.ID_ISIN = mxid.EXTIDVALUE(+)
    AND mrs.MOODYDEBTNUM(+) = mxid.MOODYDEBTNUM
    AND ext.ext_id_value in (select * from TABLE (cast (instr_ids AS INSTR_EXT_ID_T) ))
    AND b.PRICE_SRCE = p_price_srce
    AND b.PRICE_TYPE = p_price_type
    AND b.PRICE_DATE = p_price_date
    RETURN lv_bondprice_rfc;
    END f_bond_price_w_bb_stat;

  • Where clause causing a query to slow down in a cursor

    I have a table "the_table" with about 10,000 rows and four columns (id, description, inventory, and category).
    This query returns all rows immediately:
        select id
             , description
             , inventory
        from the_table
        where category =  nvl(null, category);
    However, when it is put into a cursor, like this:
    (p_user_category is a user-defined variable which can be null if the user wants all of the rows)
        c_results  sys_refcursor;
        open c_results for
            select id
                 , description
                 , inventory
            from the_table
            where category =  nvl(p_user_category, category);
        fetch c_results into v_id, v_description, v_inventory;
        close c_results;
    then it takes five minutes to return even just one row when p_user_category is null.
    However, if I change the where clause to:
        where (p_user_category is null or category = p_user_category)
    then it returns all rows immediately.
    It started being a problem right around the time of the most recent update - I am running 11.2.0.2.0 (64-bit production).

    The optimizer is smart enough to recognise that null is null, so "nvl(null,category)" collapses to "category", and your predicate "category = nvl(null, category)" is transformed to "category is not null" (if it's allowed to be null) or simply disappears - so the SQL test is not the same as the PL/SQL run.
    In PL/SQL your manual rewrite is not logicall the same as the original unless you have declared category to be non-null because your first disjunct will allow rows with a null category to be reported, while the original query would lose them.
    Check the execution plans for the SQL and the PL/SQL versions.
    Regards
    Jonathan Lewis
    http://jonathanlewis.wordpress.com
    Now on Twitter: @jloracle

  • How to create a procedure to output REF CURSOR with any WHERE clause?

    I have an requirement like this: I have huge query which need to reuse in my code more than 10 times. This SQL has about 50 lines. Thing is for those 10 odd times sometimes the WHERE clause changes (columns are the same). So I cannot create a view since SQL is not static.
    I thought of writing a procedure with a WHERE_CLAUSE input para. I output a sys refcursor by adding the where clause. But I can't do it since you cannot add a where clause like that.
    i.e.
    PROCEDURE dynamyic_query (p_where_clause IN VARCHAR2, p_out_query OUT SYS_REFCURSOR ) IS
    BEGIN
      OPEN p_out_query FOR SELECT ......... FROM table WHERE || ' ' || p_where_clause;
    END;The above gives error.
    How to handle a situation like this???? Any help would be greatly appreciated.

    I tried this method:
    I created a table tab_test which has these records:
    TNAME                          TABTYPE    CLUSTERID                                                                                                                                                                  
    ABS_V4_P_ERROR_MESSAGES        TABLE                                                                                                                                                                                  
    ABS_V4_P_ORG_PARAM             TABLE                                                                                                                                                                                  
    ABS_V4_P_PARAMETER             TABLE                                                                                                                                                                                  
    ABS_V4_P_SYS_PARAM             TABLE                                                                                                                                                                                  
    ACCINTERFACE_PARAMETERS        TABLE                                                                                                                                                                                  
    ACCOUNTS                       TABLE                                                                                                                                                                                  
    ACCOUNT_EXTRACT_PERIODS        TABLE                                                                                                                                                                                  
    ACCOUNT_EXTRACT_PERIODS#       TABLE                                                                                                                                                                                  
    ACCOUNT_EXTRACT_PERIODS_1      TABLE                                                                                                                                                                                   Now I create this proc:
    PROCEDURE FORMS_TEXT_DYN_SQL_TEST(p_where_cluase IN VARCHAR2, p_out_cursor OUT SYS_REFCURSOR) IS
      v_stmt VARCHAR2(1000);
    BEGIN
      v_stmt := 'SELECT tname FROM tab_test WHERE tname LIKE ''%ABS_V4%'' AND tabtype = :x';
      OPEN p_out_cursor FOR v_stmt using p_where_cluase;
    END;I create this code block and run it:
    declare
      v_tname varchar2(200);
      out_cursor sys_refcursor;
    begin
      forms_text_dyn_sql_test('TABLE', out_cursor );
      LOOP
        fetch out_cursor INTO v_tname;
        exit when out_cursor%NOTFOUND;
        DBMS_OUTPUT.PUT_LINE(v_tname);
      END LOOP;
    end;
    /I get correct output:
    ABS_V4_P_ERROR_MESSAGES
    ABS_V4_P_ORG_PARAM
    ABS_V4_P_PARAMETER
    ABS_V4_P_SYS_PARAMHowever, when I change the proc like this:
    PROCEDURE FORMS_TEXT_DYN_SQL_TEST(p_where_cluase IN VARCHAR2, p_out_cursor OUT SYS_REFCURSOR) IS
      v_stmt VARCHAR2(1000);
    BEGIN
      v_stmt := 'SELECT tname FROM tab_test WHERE tname LIKE ''%ABS_V4%'' AND :y';
      OPEN p_out_cursor FOR v_stmt using p_where_cluase;
    END;And run this code block:
    declare
      v_tname varchar2(200);
      out_cursor sys_refcursor;
    begin
      forms_text_dyn_sql_test(' 1 = 1 ', out_cursor );
      LOOP
        fetch out_cursor INTO v_tname;
        exit when out_cursor%NOTFOUND;
        DBMS_OUTPUT.PUT_LINE(v_tname);
      END LOOP;
    end;
    /I get error:
    [1]: (Error): ORA-00920: invalid relational operator ORA-06512: at "ABS.FORMS_TEXT_DYN_SQL_TEST", line 6 ORA-06512: at line 5Looks like you can only put column_name = :z, column_name = :y type values. You cannot it seems replace it with any WHERE CLAUSE????

  • Using if logic in the where clause of a select statement

    I have a select clause. And in the select clause there is a variable all_off_trt that can be 'Y' or 'N'.
    In the where clause I want to make it so that if a form variable is checked and all_off_trt is 'Y' then
    exclude it else if the form variable isn't checked then select it no matter what all_off_trt is.
    Is there any way to include either and if statement or a case statement within the where clause to acheive this? If not is there another way of doing it?
    Basically I am looking for a case statement like this
    case
    when all_off_trt = 'Y' and mail_para.code = 'Y' then false
    else true
    end
    Message was edited by:
    Tugnutt7

    Ok, so that really doesn't solve my problem. I have 3 different fields that I need to do that with. Each combining in a select statement to print an email list, as well as other thing limiting the where clause.
    This is currently what I have, tested and working 100%.
    cursor email_cur is
         select unique p.email,s.all_off_trt,s.all_deceased,s.no_enroll
    from participant p, trialcom t, ethics s
    where p.status='A'
    and p.surname=t.surname
    and p.initials=t.initials
    and s.trial_cd = t.tricom
    and s.centre = t.centre
    and p.email is not null
    and (t.centre in (select code from mail_parameters where user_name=user and mail_para='CENTRE')
    or 'XX' in (select code from mail_parameters where user_name=user and mail_para='CENTRE'))
    and (t.tricom in (select code from mail_parameters where user_name=user and mail_para='TRIAL')
    or 'XX' in (select code from mail_parameters where user_name=user and mail_para='TRIAL'))
    and (t.role in (select code from mail_parameters where user_name=user and mail_para='ROLE')
    or 'XX' in (select code from mail_parameters where user_name=user and mail_para='ROLE'))
    and (p.country in (select code from mail_parameters where user_name=user and mail_para='COUNTRY')
    or 'XX' in (select code from mail_parameters where user_name=user and mail_para='COUNTRY'))
    and (t.represent in (select code from mail_parameters where user_name=user and mail_para='REPRESENT')
    or 'XX' in (select code from mail_parameters where user_name=user and mail_para='REPRESENT'));
    This is in a program unit that runs when a button is clicked. At the end of that I need to add on the 3 case statements that help further narrow down the selection of emails to be printed. Then it prints the emails selected from this statement into a file. So it has to be done right in the select statement. The three table variables are the all_off_trt, all_deceased, and no_enroll. The form has 3 checkboxes. One for each, that when checked (giving the variable associated with the checkboxes a value of 'Y') excludes all emails that have a 'Y' in the coresponding table variable.

  • Nested IF Statement in WHERE clause...

    Here is a really abridged sample of my package. I need to ensure that if a salesrep has a status of 'I' (for "inactive") that the next salesrep with a status of 'A' (for "active") will be selected in its place.
    Should I include a nested loop within the WHERE clause? Any tips on how I should go about it? I'm a newbie...thanks for the help..
    SELECT rsa.name rep_name
    ,rsa.attribute7 rep_phone
    ,rsa.arrtibute8 rep_ext
    FROM ra_salesreps_all rsa
    WHERE rsa.status(+) = 'A'

    I'm assuming that this is part of your procedure within a package.....
    You must be writing a CURSOR here....
    Cursors are memory locations that can store retrieved rows and then process them one by one.
    create or replace procedure my_proc
    (<example_var> IN|OUT TYPE)
    IS
    cursor my_cursor IS
    select <the fields I want>
    from <sales type table>
    where <my value> = 'A';
    BEGIN
    <how to process the cursor>
    You can write the cursor process 2 ways:
    1) FOR x in <name_of_cursor> LOOP
    (This does whatever to the data for every record already selected by the cursor)
    2) OPEN....FETCH....CLOSE
    This is only scratching the surface of cursors...you need to read about these for what you are doing.
    Question though...is this something you need to run for a SQL process in Oracle Applications??....if so you don't need to write a package/ procedure. You can write a cursor and load it as type SQL*PLUS, and it will run for you.
    null

  • Using bind variable in dynamic where clause and concatenate with query

    Hi,
    In my procedure i am framing where clause dynamically with bind variable,When i am concatenate this with my sql query for REF CURSOR i got sql command not properly ended exception.
    Is it possible to pass values to the bind variable through the dynamic variable/value?
    Please advise
    Thanks in advance
    Siva
    IF in_applicationId IS NOT NULL THEN
              optional_where := optional_where || ' AND a.APPLICATION_ID like '||':e%';
              optional_using := ' in_appFuncGroup'||',in_currentUserID'||',in_appFuncGroup'||',in_queue_id'||',in_applicationId';
         END IF;
    My query is like this
    open Out_Main FOR SelectQuery USING optional_using

    Thanks for reply,
    In my procedure, i suppose to frame the where clause with bind dynamically according to the input parameters. so that i am framing the values of the bind variables also dynamically like this,
    Please advise...
    IF in_assignedAppFlag IS NOT NULL THEN
              IF in_assignedAppFlag = 'Y' THEN
                   optional_where := optional_where || ' AND b.ASSIGNED_TO = :b' ;
              optional_using := ' in_appFuncGroup'||',in_currentUserID';          
              ELSe
                   IF in_isSupervisor = 0 THEN
                        optional_where := optional_where || ' AND (b.ASSIGNED_TO = :b'||' OR b.ASSIGNED_TO = ''-1'' OR b.ASSIGNED_TO IS NULL)';
              optional_using := ' in_appFuncGroup'||',in_currentUserID';
                   END IF;
              END IF;
         ELSE
              IF in_isSupervisor = 0 THEN
                   optional_where := optional_where || ' AND (b.ASSIGNED_TO = :b'||' OR b.ASSIGNED_TO = ''-1'' OR b.ASSIGNED_TO IS NULL)';
                   optional_using := ' in_appFuncGroup'||',in_currentUserID';
              END IF;
         END IF;
         IF in_appFuncGroup IS NOT NULL THEN
              optional_where := optional_where || ' AND e.APP_FUNC_GROUP= :c';
              optional_using := ' in_appFuncGroup'||',in_currentUserID'||',in_appFuncGroup';
         END IF;
         IF in_queue_id IS NOT NULL THEN
              optional_where := optional_where || ' AND b.QUEUE_ID = :d';
              optional_using := ' in_appFuncGroup'||',in_currentUserID'||',in_appFuncGroup'||',in_queue_id';
         END IF;
         IF in_applicationId IS NOT NULL THEN
              optional_where := optional_where || ' AND a.APPLICATION_ID like '||':e%';
              optional_using := ' in_appFuncGroup'||',in_currentUserID'||',in_appFuncGroup'||',in_queue_id'||',in_applicationId';
         END IF;
         IF in_sourceCode IS NOT NULL THEN
              optional_where := optional_where || ' AND e.APP_SOURCE_CODE like '||':f%';
              optional_using := ' in_appFuncGroup'||',in_currentUserID'||',in_appFuncGroup'
              ||',in_queue_id'||',in_applicationId'||',in_sourceCode';
         END IF;
         IF in_logo IS NOT NULL THEN
              optional_where := optional_where || ' AND appProds.PRODUCT_TYPE like '||':g%';
              optional_using := ' in_appFuncGroup'||',in_currentUserID'||',in_appFuncGroup'
              ||',in_queue_id'||',in_applicationId'||',in_sourceCode'||',in_logo';
         END IF;
         IF in_firstName IS NOT NULL THEN
              optional_where := optional_where || ' AND upper(a.FIRST_NAME) like upper(:h%)';
              optional_using := ' in_appFuncGroup'||',in_currentUserID'||',in_appFuncGroup'
              ||',in_queue_id'||',in_applicationId'||',in_sourceCode'||',in_logo'||',in_firstName';
         END IF;
         IF in_surName IS NOT NULL THEN
              optional_where := optional_where || ' AND upper(a.SURNAME) like upper(:i%)';
              optional_using := ' in_appFuncGroup'||',in_currentUserID'||',in_appFuncGroup'
              ||',in_queue_id'||',in_applicationId'||',in_sourceCode'||',in_logo'||',in_firstName'||',in_surName';
         END IF;
         IF in_retreival_id IS NOT NULL THEN
              optional_where := optional_where || ' AND e.RETREIVAL_ID like :j%';
              optional_using := ' in_appFuncGroup'||',in_currentUserID'||',in_appFuncGroup'
              ||',in_queue_id'||',in_applicationId'||',in_sourceCode'||',in_logo'||',in_firstName'||',in_surName'||',in_retreival_id';
         END IF;

  • Dynamic where clause in my query

    I am using a view
    select * from vw_pt_inv_customer
    My requirement is have a web page where users can search for customers by filling in a form which has the following feilds: - User can fill in the form with all the feilds or not. I want a dynamic where clause.. how can i get this work for me?
    Title
    Firstname
    Surname
    Address1
    Address2
    Postcode
    Telephone No

    879796 wrote:
    I am using a view
    select * from vw_pt_inv_customer
    My requirement is have a web page What web frame work is used? Apex? Something else?
    where users can search for customers by filling in a form which has the following feilds: - User can fill in the form with all the feilds or not. I want a dynamic where clause.. how can i get this work for me?Dynamic where clauses are a Very Bad Idea (tm).
    And having an open ended search function on a web page is also not a great idea.
    If you are using Apex, the for performance (and even query flexibility) it will be better creating a separate reporting region for each unique query. A boolean rendering condition checks the existing bind variables in order to determine if that specific reporting region should be executed.
    This results in a reporting region having a proper and dedicated SQL query (no hacking of the where clause) and that region only being rendered for the proper combination of supplied filter criteria.
    A slightly more complex, but more flexible approach, is using a generic reporting region that calls a PL/SQL function that creates returns the SQL query for execution and rendering. The approach to this is very similar to constructing a SQL ref cursor (dynamically) for a client. The only difference is that instead of creating the actual ref cursor, the code simply needs to return the SQL source code statement - with bind variables. The Apex run-time does the rest (does the binds and execution and rendering).
    If you are not using Apex - you should still consider these approaches. And not hacking a single SQL to cater for all different types of filter conditions.

  • Function use in where clause

    hi,
    can we use output of a function in where clause directly ?
    i.e
    select node(condition1) clause from dual;
    clause
    occupation='SALARY'
    then i want to use this output i.e clause string directly in select stmt.
    select * from abc_table where clause
    more strictly
    select * from abc_table where (select node(condition1) clause from dual)
    which is interpreted as
    select * from abc_table where occupation='SALARY'
    Is there any way to use functions in clause area ???
    I tried CURSOR but not workd for this
    Thanks in advance,
    Rup

    I cant really understand your problem
    --fn1 is a function
    sql>
    select fn1
    from dual;
    FN1 
    CLERK 
    sql>
    select * from emp
    where job = fn1;
    EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO 
    7369  SMITH  CLERK  7902  17-DEC-80  800     20 
    7876  ADAMS  CLERK  7788  23-MAY-87  1100     20 
    7900  JAMES  CLERK  7698  03-DEC-81  950     30 
    7934  MILLER  CLERK  7782  23-JAN-82  1300     10
    Message was edited by:
            jeneesh                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • How to pass parameters to function in  where clause....

    I have a select statement like this
    SELECT XX_AIR_TICKET_EMP_BAL(:P2) from dual
    is it possible to pass to variable p2 from where clause
    like
    SELECT XX_AIR_TICKET_EMP_BAL(:P2) from dual
    where :p2 = :p1
    user can enter only in p1 so that p2 should get passed.
    Reson for this wierd requirement:In Oracle apps i cant use bind variable in select clause value set..? please ignore this reason if u are not in oracle apps...
    Tell me the solution..
    Is there any way...
    setting some env variavle aor anything

    [email protected] wrote:
    I have a select statement like this
    SELECT XX_AIR_TICKET_EMP_BAL(:P2) from dualWhy a SELECT statement? This calls the SQL engine. The function is PL/SQL. This now needs the SQL engine to context switch to the PL engine. The PL engine runs and returns a value (across memory boundaries) to the SQL engine. The SQL engine now needs to return that value as part of the output from a SQL cursor.
    Why do all this? Surely it is a lot easier and better for performance to call the PL engine directly using an anonymous PL block. E.g.
    begin
      :P3 := XX_AIR_TICKET_EMP_BAL(:P2);
    end;
    is it possible to pass to variable p2 from where clause
    like
    SELECT XX_AIR_TICKET_EMP_BAL(:P2) from dual
    where :p2 = :p1Why?
    This can be done as part of the PL block as follows:
    begin
      if :P2 = :P1 then
        :P3 :=  XX_AIR_TICKET_EMP_BAL(:P2);
      end if;
    end;But then why construct conditional statements dynamically?
    It would be a lot easier to have a new function that does the condition, e.g.
    create or replace function getAirTicket( p1 number, p2 number ) return number is
    begin
      if p1 = p2 then
        return( XX_AIR_TICKET_EMP_BAL(p2) );
      else
       return( null );
      end if;
    end;

  • Order by clause in cursor problem

    Hello,
    I'm unable to compile package body when i'm using order by clause in cursor subquery in stored procedure.
    Sample code:
    CREATE PACKAGE Announces AS
    TYPE tRefCur IS REF CURSOR;
    PROCEDURE TopAnnounces(
    iiCount IN NUMBER,
    osAnnounces OUT tRefCur,
    oiRetVal OUT NUMBER
    END Announces;
    CREATE PACKAGE BODY Announces AS
    PROCEDURE TopAnnounces(
    iiCount IN NUMBER,
    osAnnounces OUT NUMBER,
    oiRetVal OUT NUMBER
    AS
    BEGIN
    OPEN osAnnounces FOR
    SELECT Id, Name, AnnCount FROM
    SELECT Id, Name, COUNT(CategoryId) AS AnnCount FROM tblAnnounces
    GROUP BY Id, Name
    -- bellow is the line, where the code crash
    ORDER BY AnnCount DESC
    WHERE ROWNUM < iiCount + 1;
    oiRetVal := 0;
    EXCEPTION
    WHEN OTHERS THEN
    oiRetVal := -255;
    END TopAnnounces;
    END Announces;
    If I compile the code above I will get this error:
    PLS-00103: Encoutered the symbol "ORDER" when expecting on of the following:
    After I remark the problematic line, the compilation is successful (but not the result :).
    Is there something I'm doing wrong?
    Thanks for advice
    Vojtech Novacek
    null

    Sorry you can not use order by clause into one temporal table created by subquery.
    Put the order by clause offside of subquery.
    Atte.
    CC.
    <BLOCKQUOTE><font size="1" face="Verdana, Arial">quote:</font><HR>Originally posted by Vojtech Novacek:
    Hello,
    I'm unable to compile package body when i'm using order by clause in cursor subquery in stored procedure.
    Sample code:
    CREATE PACKAGE Announces AS
    TYPE tRefCur IS REF CURSOR;
    PROCEDURE TopAnnounces(
    iiCount IN NUMBER,
    osAnnounces OUT tRefCur,
    oiRetVal OUT NUMBER
    END Announces;
    CREATE PACKAGE BODY Announces AS
    PROCEDURE TopAnnounces(
    iiCount IN NUMBER,
    osAnnounces OUT NUMBER,
    oiRetVal OUT NUMBER
    AS
    BEGIN
    OPEN osAnnounces FOR
    SELECT Id, Name, AnnCount FROM
    SELECT Id, Name, COUNT(CategoryId) AS AnnCount FROM tblAnnounces
    GROUP BY Id, Name
    -- bellow is the line, where the code crash
    ORDER BY AnnCount DESC
    WHERE ROWNUM < iiCount + 1;
    oiRetVal := 0;
    EXCEPTION
    WHEN OTHERS THEN
    oiRetVal := -255;
    END TopAnnounces;
    END Announces;
    If I compile the code above I will get this error:
    PLS-00103: Encoutered the symbol "ORDER" when expecting on of the following:
    After I remark the problematic line, the compilation is successful (but not the result :).
    Is there something I'm doing wrong?
    Thanks for advice
    Vojtech Novacek<HR></BLOCKQUOTE>
    null

  • Using multiple values in a where clause, for values only known at runtime

    Dear all
    I am creating a PL/SQL program which returns multiple rows of data but only where it meets a set id values that a user has previously chosen. The id values are stored in an associative array and are chosen by a user in the preceding procedure at run time.
    I know all the table and column names in advance. The only things I don't know are the exact number of ids selected from the id column and what their values will be. This will only be known at runtime. When the procedure is run by the user it prints multiple rows of data to a web browser.
    I have been reading the following posting, which I understand to a large extent, Query for multiple value search But I cannot seem to figure out how I would apply it to my work as I am dealing with multiple rows and a cursor.
    The code as I have currently written it is wrong because I get an error not found message in my web browser. I think the var_user_chosen_map_list_ids in the for cursor loop could be the problem. I am using the variable_user_chosen_map_list_ids to store all the id values from my associatative array as a string. Which I modified from the code that vidyadhars posted in the other thread.
    Should I be creating a OPEN FOR ref cursor and if so where would I put my associative array into it? At the moment I take the value, turning it into a string and IN part in the WHERE clause holds the string, allowing the WHERE clause to read all the values from it. I would expect the where clause to read everything in the string as 1 complete string of VARCHAR2 data but this would not be the case if this part of the code at least was correct. The code is as follows:
    --Global variable section contains:
    var_user_chosen_map_list_ids VARCHAR2(32767);
    PROCEDURE PROCMAPSEARCH (par_user_chosen_map_list_ids PKG_ARR_MAPS.ARR_MAP_LIST)
    IS
    CURSOR cur_map_search (par_user_chosen_map_list_ids IN NUMBER)
    IS
    SELECT MI.map_date
           MT.map_title,
    FROM map_info MI,
         map_title MT,
    WHERE MI.map_title_id = MT.map_title_id
    AND MI.map_publication_id IN
    (var_user_chosen_map_list_ids);
    var_map_list_to_compare VARCHAR2(32767) := '';
    var_exe_imm_map VARCHAR2(32767);
    BEGIN
    FOR rec_user_chosen_map_list_ids IN 1 .. par_user_chosen_map_list_ids.count
    LOOP
       var_user_chosen_map_list_ids := var_user_chosen_map_list_ids ||
       '''' ||
       par_user_chosen_map_list_ids(rec_user_chosen_map_list_ids) ||
    END LOOP;
    var_user_chosen_map_list_ids := substr(var_user_chosen_map_list_ids,
                                            1,
                                            length(var_user_chosen_map_list_ids)-1);
    var_exe_imm_map := 'FOR rec_search_entered_details IN cur_map_search
    LOOP
    htp.print('Map date: ' || cur_map_search.map_date || ' Map title: ' || cur_map_search.map_title)
    END LOOP;';
    END PROCMAPSEARCH;EXECUTE IMMEDIATE var_exe_imm_map;
    I would be grateful of any comments or advice.
    Kind regards
    Tim

    I would like to thank everyone for their kind help.
    I have now successfully converted my code for use with dynamic SQL. Part of my problem was getting the concept confused a little, especially as I could get everything work in a static cursor, including variables, as long as they did not contain multiple values. I have learnt that dynamic sql runs the complete select statement at runtime. However even with this I was getting concepts confused. For example I was including variables and the terminator; inside my select string, where as these should be outside it. For example the following is wrong:
         TABLE (sys.dbms_debug_vc2coll(par_user_chosen_map_list_ids))....
    AND MI.map_publication_id = column_value;';Where as the following is correct:
         TABLE (sys.dbms_debug_vc2coll('||par_user_chosen_map_list_ids||'))....
    AND MI.map_publication_id = column_value';PL/SQL is inserting the values and then running the select statement, as opposed to running the select statement with the variables and then accessing the values stored in those variables. Once I resolved that it worked. My revised code is as follows:
    --Global variable section contains:
    var_user_chosen_map_list_ids VARCHAR2(32767);
    var_details VARCHAR(32767);
    PROCEDURE PROCMAPSEARCH (par_user_chosen_map_list_ids PKG_ARR_MAPS.ARR_MAP_LIST)
    IS
    BEGIN
    FOR rec_user_chosen_map_list_ids IN 1 .. par_user_chosen_map_list_ids.count
    LOOP
       var_user_chosen_map_list_ids := var_user_chosen_map_list_ids ||
       '''' ||
       par_user_chosen_map_list_ids(rec_user_chosen_map_list_ids) ||
    END LOOP;
    var_user_chosen_map_list_ids := substr(var_user_chosen_map_list_ids,
                                            1,
                                            length(var_user_chosen_map_list_ids)-1);
    var_details := FUNCMAPDATAFIND (var_user_chosen_map_list_ids);
    htp.print(var_details);
    END PROCMAPSEARCH;
    FUNCTION FUNCMAPDETAILS (par_user_chosen_map_list_ids IN VARCHAR2(32767)
    RETURN VARCHAR2
    AS
    TYPE cur_type_map_search IS REF CURSOR;
    cur_map_search cur_type_map_search;
    var_map_date NUMBER(4);
    var_map_title VARCHAR2(32767);
    begin:
    OPEN cur_map_search FOR
    'SELECT MI.map_date,
           MT.map_title
    FROM map_info MI,
         map_title MT,
         TABLE (sys.dbms_debug_vc2coll(' || par_user_chosen_map_list_ids || '))
    WHERE MI.map_title_id = MT.map_title_id
    AND MI.map_publication_id = column_value';
    LOOP
    FETCH cur_map_compare INTO
    var_map_date,
    var_map_title;
    var_details := var_details || 'Map date: '||
                        var_map_date ||
                        'Map title: ' ||
                        var_map_title;
    EXIT WHEN cur_map_compare%NOTFOUND;
    END LOOP;
    RETURN var_details;
    END FUNCMAPDETAILS;Kind regards
    Tim

Maybe you are looking for

  • Table for finding out the User details

    Hi All, We need to create a KPI for number of hours a user has looged into a system. Is there any specific table where all the user related information is captured.If the information is not present in a table where  can we find the above information?

  • HP Pavilion tx2500z - Default Function Keys not working

    I have a HP Pavilion tx2500z (model: KD436AV) laptop with Windows 7 64 bit OS installed. Since last few months, I am not able to use default function keys (F1, F2...F12) and other keys at the top row of the keyboard, including Pg Up, Pg Down, Home, E

  • Trim Down The ROI of an image

    Hello folks, i am working on a image processing project which involves template matching. Thanks to the guidance of the LabVIEW experts i have been able to complate the template matching and implement a state machine. i am trying restrict my Region O

  • Is There A Way To Normalize The Volume Of My Library Without "Sound Check"?

    Is there a way to put the volume of my entire library at say, +1.0 dB? It's annoying to have music that's at -5.7 and then listen to something that's at +2.0 and constantly having to adjust the volume. I have Sound Check enabled on my iPod but it rea

  • Download app onto 2 computers with same iTunes account

    If I purchase an app to install onto my iphone from my computer can my daughter also download the app to her computer and install onto her iPod without re-purchasing? We share the iTunes account. If this is possible then what do you do when the app s