Dynamic where clause with loop statement

Hi all,
is it possible to use a dynamic where clause with a loop statement?
Can you please advise me, how the syntax needs to be?
Thanks for your suggestions,
kind regards, Kathrin!

Hi Kathrin,
           If u are in ECC 6.0, please go through the code...
          REPORT  zdynamic_select.
TYPES:
  BEGIN OF ty_sales,
    vbeln  TYPE vbak-vbeln,            " Sales document
    posnr  TYPE vbap-posnr,            " Sales document item
    matnr  TYPE vbap-matnr,            " Material number
    arktx  TYPE vbap-arktx,            " Short text for sales order item
    kwmeng TYPE vbap-kwmeng,           " Order quantity
    vkorg TYPE vbak-vkorg,             " Sales organization
    kunnr TYPE vbak-kunnr,             " Sold-to party
    netwr TYPE vbak-netwr,             " Net Value of the Sales Order
  END OF ty_sales.
DATA :
  gt_sales TYPE STANDARD TABLE OF ty_sales,
  wa_sales TYPE ty_sales.
DATA: ob_select TYPE REF TO cl_rs_where.
DATA: ob_from   TYPE REF TO cl_rs_where.
DATA: ob_where  TYPE REF TO cl_rs_where,
      gv_source TYPE abapsource.
START-OF-SELECTION.
*Step 1 : Prepare the select fields.
  PERFORM zf_build_select.
*Step 2 : Build the from clause for the select
  PERFORM zf_build_from.
*Step 3 : Build the where clause for the select
  PERFORM zf_build_where.
*Step 4 : Execute the dynamic select
  SELECT (ob_select->n_t_where)
      FROM (ob_from->n_t_where)
        INTO CORRESPONDING FIELDS OF TABLE gt_sales
        WHERE (ob_where->n_t_where).
  LOOP AT gt_sales INTO wa_sales.
    WRITE :   /5 wa_sales-vbeln,
              15 wa_sales-vkorg,
              20 wa_sales-kunnr,
              40 wa_sales-netwr,
              50 wa_sales-posnr,
              60 wa_sales-matnr,
              70 wa_sales-arktx,
              90 wa_sales-kwmeng.
  ENDLOOP.
*&      Form  zf_build_select
FORM zf_build_select .
  CREATE OBJECT ob_select.
*Build the table name/field name combination
*Add Sales order header fields
  CLEAR gv_source.
  CALL METHOD cl_rs_where=>build_tabname_fieldname
    EXPORTING
      i_tabname   = 'VBAK'
      i_fieldname = 'VBELN'
      i_sign      = '~'
    IMPORTING
      e_combined  = gv_source.
*Add the where line
  CALL METHOD ob_select->add_line
    EXPORTING
      i_line = gv_source.
  CLEAR gv_source.
  CALL METHOD cl_rs_where=>build_tabname_fieldname
    EXPORTING
      i_tabname   = 'VBAK'
      i_fieldname = 'VKORG'
      i_sign      = '~'
    IMPORTING
      e_combined  = gv_source.
*Add the where line
  CALL METHOD ob_select->add_line
    EXPORTING
      i_line = gv_source.
  CLEAR gv_source.
  CALL METHOD cl_rs_where=>build_tabname_fieldname
    EXPORTING
      i_tabname   = 'VBAK'
      i_fieldname = 'KUNNR'
      i_sign      = '~'
    IMPORTING
      e_combined  = gv_source.
*Add the where line
  CALL METHOD ob_select->add_line
    EXPORTING
      i_line = gv_source.
  CLEAR gv_source.
  CALL METHOD cl_rs_where=>build_tabname_fieldname
    EXPORTING
      i_tabname   = 'VBAK'
      i_fieldname = 'NETWR'
      i_sign      = '~'
    IMPORTING
      e_combined  = gv_source.
*Add the where line
  CALL METHOD ob_select->add_line
    EXPORTING
      i_line = gv_source.
*Add Sales order item fields
  CALL METHOD cl_rs_where=>build_tabname_fieldname
    EXPORTING
      i_tabname   = 'VBAP'
      i_fieldname = 'POSNR'
      i_sign      = '~'
    IMPORTING
      e_combined  = gv_source.
*Add the where line
  CALL METHOD ob_select->add_line
    EXPORTING
      i_line = gv_source.
  CLEAR gv_source.
  CALL METHOD cl_rs_where=>build_tabname_fieldname
    EXPORTING
      i_tabname   = 'VBAP'
      i_fieldname = 'MATNR'
      i_sign      = '~'
    IMPORTING
      e_combined  = gv_source.
*Add the where line
  CALL METHOD ob_select->add_line
    EXPORTING
      i_line = gv_source.
  CLEAR gv_source.
  CALL METHOD cl_rs_where=>build_tabname_fieldname
    EXPORTING
      i_tabname   = 'VBAP'
      i_fieldname = 'ARKTX'
      i_sign      = '~'
    IMPORTING
      e_combined  = gv_source.
*Add the where line
  CALL METHOD ob_select->add_line
    EXPORTING
      i_line = gv_source.
  CLEAR gv_source.
  CALL METHOD cl_rs_where=>build_tabname_fieldname
    EXPORTING
      i_tabname   = 'VBAP'
      i_fieldname = 'KWMENG'
      i_sign      = '~'
    IMPORTING
      e_combined  = gv_source.
*Add the where line
  CALL METHOD ob_select->add_line
    EXPORTING
      i_line = gv_source.
ENDFORM.                    " zf_build_select
*&      Form  zf_build_from
FORM zf_build_from .
  CREATE OBJECT ob_from.
*Add opening bracket
  CALL METHOD ob_from->add_opening_bracket
  CLEAR gv_source.
*Add the join condition.This can be made
*fully dynamic as per your requirement
  gv_source = 'VBAK AS VBAK INNER JOIN VBAP AS VBAP'.
*Add the where line
  CALL METHOD ob_from->add_line
    EXPORTING
      i_line = gv_source.
  CLEAR gv_source.
*Add the join condition.This can be made
*fully dynamic as per your requirement
  gv_source = 'ON VBAKVBELN = VBAPVBELN'.
*Add the where line
  CALL METHOD ob_from->add_line
    EXPORTING
      i_line = gv_source.
*Add the closing bracket
  CALL METHOD ob_from->add_closing_bracket
ENDFORM.                    " zf_build_from
*&      Form  zf_build_where
FORM zf_build_where .
  DATA :
  lv_field TYPE REF TO data,
  lv_field_low TYPE REF TO data,
  lv_field_high TYPE REF TO data.
  CREATE OBJECT ob_where.
*Add the field VBELN : Sales Document
*Use this method if you want to assign a single value to a field
*Set the value for VBELN : Sales Document Number
CALL METHOD ob_where->add_field
   EXPORTING
     i_fieldnm  = 'VBAK~VBELN'
     i_operator = '='
     i_intlen   = 10
     i_datatp   = 'CHAR'
   IMPORTING
     e_r_field  = lv_field.
CALL METHOD ob_where->set_value_for_field
   EXPORTING
     i_fieldnm = 'VBAK~VBELN'
     i_value   = '0000120020'.
*Use this method if you want to assign a range of values
*Set a range for the Sales Document number
  CALL METHOD ob_where->add_field_between_2values
    EXPORTING
      i_fieldnm      = 'VBAK~VBELN'
      i_intlen       = 10
      i_datatp       = 'CHAR'
    IMPORTING
      e_r_field_low  = lv_field_low
      e_r_field_high = lv_field_high.
  CALL METHOD ob_where->set_2values_for_field
    EXPORTING
      i_fieldnm    = 'VBAK~VBELN'
      i_value_low  = '0000120020'
      i_value_high = '0000120067'.
*Set the 'AND' Clause
  CALL METHOD ob_where->add_and.
*Add the field MATNR : Material
  CALL METHOD ob_where->add_field
    EXPORTING
      i_fieldnm  = 'MATNR'
      i_operator = '='
      i_intlen   = 18
      i_datatp   = 'CHAR'
    IMPORTING
      e_r_field  = lv_field.
*Set the value for the Material field
  CALL METHOD ob_where->set_value_for_field
    EXPORTING
      i_fieldnm = 'MATNR'
      i_value   = '000000000050111000'.
*Set the 'AND' Clause
  CALL METHOD ob_where->add_and
*Add the field VKORG
  CALL METHOD ob_where->add_field
    EXPORTING
      i_fieldnm  = 'VKORG'
      i_operator = '='
      i_intlen   = 4
      i_datatp   = 'CHAR'
    IMPORTING
      e_r_field  = lv_field.
*Set the value for VKORG : Sales Organization
  CALL METHOD ob_where->set_value_for_field
    EXPORTING
      i_fieldnm = 'VKORG'
      i_value   = 'GMUS'.
ENDFORM.                    " zf_build_where

Similar Messages

  • Dynamic WHERE clause in SELECT statement

    Hi,
    I need to extract (SELECT) all the products in different salesorganizations. Since a product can be available in more than 1 salesorg I have created several properties in the PRODUCT dimension - 1 for each salesorganization (naming: Sxxxx where xxxx is the salesorganization number).
    Since I need to prefix the salesorganization property with an "S" I have created a property on the SALESORG dimension called SALESORG.
    Therefore I need to create a dynamic WHERE clause in the SELECT statement. Currently my script is:
    *SELECT(%SORG%, "[SALESORG]",SALESORG, [ID]=%SALESORG_SET%)
    *SELECT(%PROD%, "[ID]",PRODUCT, [%SORG%]="X")
    My first SELECT find the Sxxx (equal to the property I need in the PRODUCT dimension). My second SELECT uses the variable in the first SELCT statement to use the correct property for the WHERE clause.
    Unfortunately the code is not validated - any suggestions?
    /Lars

    Hi Lars,
    If you run it from a DM package without validating it, does it still work? I would bet it does.
    If this is the case I would open a message with SAP (it would be an enhancement request). Until they fix the validation code, you would just have to live with the script not validating.
    Cheers,
    Ethan

  • Creating dynamic where clause with string delimiter " ; "

    hi...
    i need a solution for complex problem like, 1. start_date IN date,
    end_date IN date,
    shift_type IN varchar2
    i will get shift_type as "first_shift" or "first_shift;second_shift" or "first_shift;second_shift;third_shift" ....etc any combination. where fist & second & third shits are nothing but 1 , 2 , 3. i need to find out data between start_date and end_date in combination of shifts ( may be 1;2 or 1;3 or 1;2;3 or 2;3 ...etc) . now i need to write this code in dynamic where clause ...i tried in different ways...but not succeeded. can anybody guide me step by step...or with script.
    NOTE: there is a table called "shift" with data like
    shift_type shift_mode
    1 first_shift
    2 second_shift
    3 third_shift

    Hi,
    Whenever you have a problem, post a little sample data (CREATE TABLE and INSERT statements) and the results you want from that data.
    If the question involves parameters, give a few different sets of parameters and the results you want for each set, given the same sample data.
    It's unclear that you need dynamic SQL at all.
    If shift_type is a variable, that can be either a single value or a ;-delimited list (such as '1;3'), you can compare that to a column called shift_column like this:
    WHERE        ';' || shift_type   || ';'     LIKE
           '%;' || shift_column || ';%'No dynamic SQL or PL/SQL required.
    If you really do want to use dynamic SQL, these two pages should gives you some ideas:
    http://www.oracle-base.com/articles/misc/DynamicInLists.php
    http://tkyte.blogspot.com/2006/06/varying-in-lists.html

  • Dynamic where clause in Loop

    Is iot possible to use a dynamic where clause in a loop?  If so how to you code this?
    i.e. Loop at itab in wa where (dynamic_where_clause)...
        endloop...
    Moderator message: please read ABAP documentation for your SAP release.
    Edited by: Thomas Zloch on Mar 29, 2011 3:20 PM

    You should be aware that a LOOP AT ... WHERE is not an optimized access .
    + because on a standard table there is not sort order,
       so the loop goes over the whole table and does  
       something when the condition is fulfilled
    + it is optimzed for sorted tables, but only for one key!
    => so your idea must have bad performance!
    If this is not a problem, then you can solve the problem
    in the following way:
    loop at table
       if ( dynamic condition )
       endif.
    endloop.
    But maybe is dynamic condition is not necessary, but
    a some fixed if-conditions are also o.k.
    Siegfried

  • How to use where clause with get statement in LDB programs

    Hi All,
    I am using logical databse in my report program.I am not getting how to use the where clause in the get statement is it possible to use?or if not possible only option is we should filter it after get statment is right?Can you please some body throw some idea on this?
    Regards
    Mahesh

    Hi,
    Reffer these links
    http://help.sap.com/saphelp_nw04/helpdata/en/9f/db9bfa35c111d1829f0000e829fbfe/content.htm
    http://help.sap.com/saphelp_nw04/helpdata/en/9f/db9b5e35c111d1829f0000e829fbfe/frameset.htm
    http://help.sap.com/saphelp_nw04/helpdata/en/c6/8a15381b80436ce10000009b38f8cf/frameset.htm
    /people/srivijaya.gutala/blog/2007/03/05/why-not-logical-databases
    reward if helpful
    Thanks,
    Suma.

  • Loop with dynamic where clause

    hi,
        In a program we need to use a loop into an internal table with a where condition is decided on run time.
    "Select" from database table is using dynamic where clause and it is working fine.
    For the loop part does any one have any idea on this? if we can have dynamic where clause in a loop into internal tab?
    Thanks

    Hi,
    You can't use 'loop where' dynamically  .
    Have you already tried to use Ranges and CHECK statement?
    Like:
    First mount range options;
    Loop at <table>.
    Check using the range.
    endloop.
    Maybe it's works for you !!
    Marcelo Ramos

  • Dynamic query in where clause while looping in an internal table.

    Hi,
    Had a small question : How can i make a dynamic query for the WHERE clause while looping at an internal table.
    i want to implement a dynamic where clause query for the below example.
    it_cfx_col is an internal table and wa_cfx_col is a work area for it_cfx_col
      DATA :
      i_cfx_col TYPE TABLE OF cfx_col,
      wa_cfx_col LIKE LINE OF i_cfx_col.
    DATA : count TYPE i VALUE 0.
    DATA : l_where_clause TYPE string,
             l_where_clause2 TYPE string,
             l_name type string.
    l_name = 'NANDANOM'.
    l_scenario = 'collaboration'.
    LOOP AT it_cfx_col INTO wa_cfx_col
    WHERE CREATED_BY = l_name
    AND SCENARIO = l_scenario.
    count = count + 1.
    some business logic implemented using the work area wa_cfx_col
    endloop.
    Now i want to write a dynamic query for the where clause.
    DATA : count TYPE i VALUE 0.
      DATA : l_where_clause TYPE string,
             l_where_clause2 TYPE string,
             l_name type string.
    l_name = 'NANDANOM'.
    l_scenario = 'collaboration'.
      l_where_clause = 'CREATED_BY = l_name'.
      l_where_clause2 = 'AND SCENARIO = l_scenario'.
    if l_scenario is not initial.
      CONCATENATE  l_where_clause l_where_clause2
      INTO l_where_clause SEPARATED BY space.
    endif.
    LOOP AT i_cfx_col INTO wa_cfx_col
    WHERE (l_where_clause).
    count = count + 1.
    some business logic implemented using the work area wa_cfx_col
    endloop.
    when i compile this i get an error message as { Statement concluding with "...(l_where_clause)" ended unexpectedly}
    Even i changed the initilization of the variable  l_where_clause2 to [ l_where_clause2 = 'AND SCENARIO = l_scenario.'. ]
    added the end of line demarkation ".", but still i got the same error message.
    Is it a limtation in ABAP that i cannot write a dynamic query for the where clause while looping at an internal table?
    Regards,
    om

    Hi savita,
    there in no such 1 limitaion in abap for dynamic query .. i think the  error meassge is only beacuse of your synatx delcartaion.
    >> LOOP AT i_cfx_col INTO wa_cfx_col
       WHERE (l_where_clause).
       count = count + 1.
    some business logic implemented using the work     area    wa_cfx_col
       endloop.
    afted delclarataion also , in the where statement you should specify both the field name and value bname
       LOOP AT i_cfx_col INTO wa_cfx_col
       WHERE l_where_clause = 'CREATED_BY = l_name' .
       count = count + 1.
    hope it helps.
    regads
    priya.

  • How to add a dynamic where clause for a sql based VO with group by query?

    Hi,
    Here is my case, I have a sql query based VO with the query like "select status, count(*) StatusCount from my_table group by status". Now I used the following java code trying to dynamically add the where clause to my VO to filter the rows based the type attribute in my DB table.
    vo.setWhereClause("type='MyType1' ");
    vo.executeQuery();
    Then I got the sql syntax error. Looks like the ADF has added the where clause to the end of my sql so my sql becomes "select status, count(*) StatusCount from my_table group by status where type='MyType1' ". But what I expected was the correct syntax "select status, count(*) StatusCount from my_table where type='MyType1' group by status".
    Does anyone know if this is an ADF bug? Or is there any other way to achieve my goal?
    Thanks,
    Chunyang
    Edited by: Chunyang on Dec 13, 2012 9:09 PM

    Hi,
    When you use setWhereClause on the VO, it is applied on top of the VO query. I.e, assume your VO has the following query.
    select empno, ename from empNow, if you apply the where clause programatically, only the two attributes that you are using in the select statement could be used. I.e
    select * from (select empno, ename from emp) where ename='KING' - VALID
    select * from (select empno, ename from emp) where deptno=10  - INVALID (because the inner query - the one you've defined as query for your vo does not have deptno attribute selected)If you would need to set a dynamic where clause, you need to make them available in your select statement / use bind variables.
    -Arun

  • Dynamic itab with Dynamic Where clause

    Hi, Dear All,
    Can someone provide a code extract for Dynamic where clause, i had already done with dynamic itab for a given set of fields, and i need to add where clause dynamically for a given field for a given range of values.
    select (i_fields) into table <dyn_table>
                      from (p_table)
                     where (v_where).
    In the above except the where clause, everything is done. Please help me.
    with best regards
    Mahesh

    Hi,
    here is the code extract for your reference.Pl. correct me.
    with regards
    REPORT  Z_DYN_ITAB                              .
    TYPE-POOLS: SLIS.
    DATA:NAME(100) TYPE C.
    TYPES: BEGIN OF ITAB_TYPE,
            WORD(20),
          END   OF ITAB_TYPE.
    DATA: ITAB TYPE STANDARD TABLE OF ITAB_TYPE WITH HEADER LINE.
    DATA: vg_fields(255) TYPE c,
          i_fields LIKE TABLE OF vg_fields.
    DATA: it_fcat TYPE slis_t_fieldcat_alv,
          is_fcat LIKE LINE OF it_fcat,
          ls_layout TYPE slis_layout_alv.
    DATA: it_fieldcat TYPE lvc_t_fcat,
          is_fieldcat LIKE LINE OF it_fieldcat.
    field-symbols: <dyn_table> type standard table,
                   <dyn_wa>,
                   <dyn_field>.
    data: dy_table type ref to data.
    data: dy_line type ref to data,
          xfc type lvc_s_fcat.
    DATA: v_where TYPE string, " Variable for storing where clause.
          v_dynamic(18) TYPE c, "variable to store select option datatype
          o_field TYPE REF TO cx_root," object to catch exception
          text TYPE string. "string variable to store exception text.
    CONSTANTS: c_var(15) TYPE c VALUE ' IN S_RANGE'.
    selection-screen begin of block b1 with frame.
    parameters: p_table(30) type c default 'T001',
                p_name(100) type c,
                p_field(10) TYPE c. " Parameter to capture field name.
    SELECT-OPTIONS: s_range FOR v_dynamic. " Select-option for range.
    selection-screen end of block b1.
    start-of-selection.
    NAME = p_name.
    SPLIT NAME AT ',' INTO TABLE ITAB.
    LOOP AT ITAB.
    is_fcat-fieldname = itab-word.
    is_fcat-tabname = p_table.
    APPEND is_fcat to it_fcat.
    ENDLOOP.
    LOOP AT it_fcat INTO is_fcat.
      is_fieldcat-fieldname = is_fcat-fieldname.
      is_fieldcat-tabname = is_fcat-tabname.
      APPEND is_fieldcat TO it_fieldcat.
      CONCATENATE is_fieldcat-tabname is_fieldcat-fieldname INTO
            vg_fields SEPARATED BY '~'.
      APPEND vg_fields TO i_fields.
    ENDLOOP.
    perform create_dynamic_itab.
    perform get_data.
    Create dynamic internal table and assign to FS
    form create_dynamic_itab.
    call method cl_alv_table_create=>create_dynamic_table
    exporting
    it_fieldcatalog = it_fieldcat
    importing
    ep_table = dy_table.
    assign dy_table->* to <dyn_table>.
    Create dynamic work area and assign to FS
    create data dy_line like line of <dyn_table>.
    assign dy_line->* to <dyn_wa>.
    endform.
    form get_data.
    Select Data from table.
    CONCATENATE p_field c_var INTO v_where.
    TRY.
    select (i_fields) into table <dyn_table>
                      from (p_table)
                     where (v_where).
    if sy-dbcnt = 0.
    write : text-t02.
    endif.
    *Write out data from table.
    Loop at <dyn_table> into <dyn_wa>.
    do.
    assign component sy-index of structure <dyn_wa> to <dyn_field>.
    if sy-subrc <> 0.
      exit.
    endif.
    if sy-index = 1.
      write:/ <dyn_field>.
    else.
      write: <dyn_field>.
    endif.
    enddo.
    endloop.
    Exception Catching.
    CATCH cx_root INTO o_field.
    text = o_field->get_text( ).
    Calling Function to give information message regarding Exception
    CALL FUNCTION 'POPUP_TO_INFORM'
    EXPORTING
    titel = text-t03
    txt1 = text
    txt2 = text-t04.
    TXT3 = ' '
    TXT4 = ' '
    LEAVE TO LIST-PROCESSING.
    ENDTRY.
    endform.

  • 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;

  • Where clause with a combination of And and Or statements - Basic question

    Hi,
    I have a where clause with a combination of And and Or statements... May I know which one would run first
    Here is the sample
    WHERE SITE_NAME = 'Q' AND ET_NAME IN ('12', '15') AND TEST_DATE > DATE OR SITE_NAME = 'E' AND ET_NAME IN ('19', '20')
    can you please explain how this combination works
    Thanks in advance

    Hi,
    This reminds me of a great story. It's so good, it probably didn't really happen, but it's so good, I'm going to repeat it anyway.
    IBM once had an "executive apptitude test" that they would give to job applicants. There were some questions you might call general knowlege or trivia questions, and each question had a weight (for example, answering an unimportant queestion might score one point, an important question might be 5 points.) One of the questions was "What is the standard width of a mobile home?", and the weight of the question was -20: answering the question correctly did serious harm to your score. The reasoning was that the more you knew about mobile homes, the less likely you were to be their kind of executive.
    Now, as to your question, the correct answer is: I don't know. I don't want to know. Mixing ANDs and ORs without grouping them in parentheses is a really bad idea. Even if you get it right, it's going to confuse the next person who has to look at that code. Use parentheses to make sure the code is doing what you want it to do.
    If you really want to find out, it's documented in the SQL language manual. Look up "Operators, prcedence"
    http://docs.oracle.com/cd/E11882_01/server.112/e26088/operators001.htm#sthref815
    You can easily do an experiment, using scott.emp, or even dual, where
    WHERE  (x AND y)
    OR      zproduces different results from
    WHERE   x
    AND     (y OR z)

  • How to create VO with multiple dynamic where clauses on select with UNION?

    I am trying to implement the View Object for the UNION query that looks like this:
         select a,b,c...
              from t1,t2,...
              where dynamic_where_clause1     
         union all
         select a,b,c,...
              from t11,t12, ...
              where dynamic_where_clause2
    There are up to 60 input parameters that are used to generate dynamic where clauses. They are actually created by calling PL SQL function.
    So far, I was not able to assign both where clauses to the view object. Is there a workable solution for this problem, besides resorting to programmatic View Object?
    I understand that recommended way with UNIONs is to wrap both queries into a parent select:
    select * from (
         select a,b,c...
              from t1,t2,...
              where ... -- table relationship joints
         union all
         select a,b,c,...
              from t11,t12, ...
              where ... -- table relationship joints
    ) QRSLT
         where dynamic_where_clause
    Unfortunately this approach doesn't work here, since individual selects are producing unmanageable amount of data and resulting query takes forever to complete.

    I afraid I would not have any real benefits from using VO if I replace the entire query with every request. Actually, the performance may suffer.
    I solved the problem by creating a POJO Data Control and invoking the custom select query from java. Not sure if it is the best approach to the problem, but implementation time is limited and it works.
    Actually, this is not the first time I see the need to implement VO with complicated SQL like select with unions and dynamic pieces. It would be nice to find a solution and not resort to workarounds.
    Edited by: viksicom on Aug 2, 2012 8:48 AM

  • Dynamic IF statement...like dynamic where clause...

    Hi,
    I know how to do a dynamic where clause by putting (varname) like shown below -
    select * from cust_mstr where (varname).
    Can I do it something like this in IF statement.
    if (varname).
    endif.
    The varname will have different conditions.
    Your help will be greately appreciated.
    Regards,
    Sume

    Hello Sume,
    I would try to use an approach using ranges:
    DATA:
      gv_material TYPE matnr.
    gv_material = '292-392-392-202'.
    IF gv_material = '292-392-392-202'.
      WRITE: / `Static check:`, gv_material, `matches!`.
    ELSE.
      WRITE: / `Static check:`, gv_material, `doesn't match!`.
    ENDIF.
    PERFORM dynamic_check
                USING
                   gv_material
                   'I'
                   'EQ'
                   '292-392-392-202'
                   space.
    PERFORM dynamic_check
                USING
                   gv_material
                   'I'
                   'CP'
                   '*292*'
                   space.
    PERFORM dynamic_check
                USING
                   gv_material
                   'I'
                   'CP'
                   '*ABC*'
                   space.
    *&      Form  dynamic_check
    FORM dynamic_check USING pv_value    TYPE clike
                             pv_sign     TYPE ddsign
                             pv_option   TYPE ddoption
                             pv_low      TYPE string
                             pv_high     TYPE string.
      DATA lr_range TYPE RANGE OF string.
      DATA ls_range LIKE LINE  OF lr_range.
      ls_range-sign   = pv_sign.
      ls_range-option = pv_option.
      ls_range-low    = pv_low.
      ls_range-high   = pv_high.
      APPEND ls_range TO lr_range.
      IF pv_value IN lr_range.
        WRITE: / `Dynamic check matches: `, pv_value, ls_range-sign, ls_range-option, ls_range-low, ls_range-high.
      ELSE.
        WRITE: / `Dynamic check doesn't match:`, pv_value, ls_range-sign, ls_range-option, ls_range-low, ls_range-high.
      ENDIF.
    ENDFORM.                    "dynamic_check
    For values of SIGN and OPTION check the values of the domains used in the data elements of the parameters of form dynamic_check.
    Output is like:
    Static check:                292-392-392-202    matches!
    Dynamic check matches:       292-392-392-202    I EQ 292-392-392-202
    Dynamic check matches:       292-392-392-202    I CP *292*
    Dynamic check doesn't match: 292-392-392-202    I CP *ABC*
    Edited by: Alejiandro  Sensejl on Aug 11, 2010 8:23 PM:
    Unfortunately I don't know why the code-tag isn't working, I sent a mail to SCN tech-team about this... Sorry, but you have to copy the code somehow to have a look at it

  • REPORT with dynamic WHERE CLAUSE (run RDF or REP) ?

    Hi:
    When running a REPORT (myreport.rep) with dynamic where clause using a lexical parameter, I got this error:
    REP-1439: Cannot compile .REP or .PLX file as it does not have source
    If i run the report specifiying RDF extension (myreport.rdf) the report run successfully! Is this normal ?
    If I specify RDF extension will Report Server COMPILE the report everytime I execute it ?
    When using dynamic WHERE CLAUSE I will have to run RDF files instead of REP ?
    I'm running Reports 9i under Linux, with IDS under Windows.
    Waiting Help
    Joao Oliveira

    It sounds like you are building the .rep files on one platform (windows) and running them on another (linux). The reason that the .rdf file continues to work is that Reports recompiles the PL/SQL within the report when you move from one platform to another or change schemas. .rep files can't be re-compiled in this way so you need to ensure they are compiled successfully when converting them.
    You need to convert from .rdf to .rep on the platform that you are intending to run on. Try running rwconverter on the linux platform with "compile_all=yes" to produce the .rep file and running that .rep file.

  • Dynamic where clause in "delete itab where..." variant

    Hi,  I need to be able to delete the contents of an internal table based on a dynamic where clause. I don't want to loop through the entire table to test each record.  Since the delete statement doesn't seem to allow a dynamic "where" clause I am trying to use a macro.  I can't get the macro to work either. 
    I get the following error in my syntax check:
    Statement concluding with "...FINAL_STATEMENT" ended unexpectedly.          
    Is there any way to get this macro to pass the syntax text or accomplish the dynamic delete?
    MACRO TO FILTER OUT UNWANTED RECORDS (RETAIL OUTLETS)
      DEFINE DELOUT.
        DELETE MY_ITAB WHERE &1.
      END-OF-DEFINITION.
    Example FINAL_STATEMENT = (  ZRRDIVIS = '4' )
    USING MACRO DOES NOT PASS SYNTAX CHECK
      DELOUT FINAL_STATEMENT.

    Well, if you really need this kind of functionality,  here is a code sample that I just threw together that would work.  A lot of code for one little delete statement though.
    report zrich_0001 .
    data: begin of itab occurs 0,
          fielda type c,
          fieldb type c,
          fieldc type c,
          end of itab.
    data: where_clause type string.
    class lcl_source definition.
      public section.
        types: t_source(72).
        class-data: routine(32) value 'TEMP_ROUTINE',
                    program(8),
                    message(128),
                    line type i.
        class-data: isource type table of t_source,
                    xsource type t_source.
        class-methods: execute_source importing im_clause type string.
    endclass.
    start-of-selection.
    * Build the itab.
      itab-fielda = '1'.
      itab-fieldb = '2'.
      itab-fieldc = '3'.
      append itab.
      itab-fielda = '3'.
      itab-fieldb = '2'.
      itab-fieldc = '1'.
      append itab.
    * Set the where clause
      where_clause = 'FIELDA = ''1''.'.
    * call the delete statement.
      call method lcl_source=>execute_source
                exporting
                    im_clause = where_clause.
    *       CLASS lcl_source IMPLEMENTATION
    class lcl_source implementation.
      method execute_source.
        xsource = 'REPORT ZTEMP_REPORT.'.
        insert xsource  into isource index 1.
        xsource = 'FORM & tables itab.'.
        replace '&' with routine into xsource.
        insert xsource  into isource index 2.
        xsource = 'Data: begin of xitab occurs 0,'.
        append xsource to isource.
        xsource = 'fielda type c,'.
        append xsource to isource.
        xsource = 'fieldb type c,'.
        append xsource to isource.
        xsource = 'fieldc type c,'.
        append xsource to isource.
        xsource = 'end of xitab.'.
        append xsource to isource.
        xsource = 'xitab[] = itab[].'.
        append xsource to isource.
        concatenate 'DELETE xITAB WHERE' im_clause into xsource
                      separated by space.
        append xsource to isource.
        xsource = 'ENDFORM.'.
        append xsource to isource.
        generate subroutine pool isource name program
                                 message message
                                 line line.
        if sy-subrc = 0.
          perform (routine) in program (program) tables itab.
        else.
          write:/ message.
        endif.
      endmethod.
    endclass.
    Regards,
    Rich Heilman

Maybe you are looking for

  • Problem in Career and Job

    Hi All, We are facing crictical issues in career and Job after we moved all our objects to quality machine. We are using mySAP ERP 2005 Buss pack 1.0 with ECC 6.0, Portal 7.0 SP12 and E-rec 600. The career and Job especially -  Apply Directly and can

  • How to speedup Goldengate Replicat during busy time

    both db version is 11.2.0.3 on linux redhat Target tables replicate normal during slow time, however last weekend, there are a lot activities on the source, then target lag up to 5 hours . We checked error log, reports , no errors on both source/targ

  • Oracle Blob using Java help (Error: JVM_recv in socket input stream read )

    I am trying to insert a record with Blob column (size of image 'mg.jpg' about 15KB) and I get the runtime error: [java] java.sql.SQLException: Io exception: Connection reset by peer: JVM_recv in socket input stream read [java]      at oracle.jdbc.dba

  • Networking windows Pc With mac

    I would like to share a printer that i have connected to my mac with a windows PC. The windows PC is running windows XP home eddition. I can network and share files but the problem is whene i try to share the pritner it will not print anything the pr

  • Period 0 not exist in New FIGL Cube

    We are using new FIGL Cube (0FIGL_C10) and trying the drilldown using Posting Period (0FISCPER3), however it only shows period 1 through 16 and also a period that is Null (#).  We are trying to restrict to Period 0, however this does not exist in the