Dynamic where condition in Select statement

Hi,
I have 10 fields on selection-screeen. In which ever field the user enters single values or ranges,i should pick that field dynamically and pass that field along with value range to Where condition of Select statement.How can i achieve this? Please help.
Regards
K Srinivas

see the following example:
data : begin of itab occurs 0,
         matnr like mara-matnr,
end of itab.
ypes: begin of ty_s_clause.
types:   line(72)  type c.
types: end of ty_s_clause.
data : begin of gt_condtab occurs 0.
        include structure hrcond.
data : end   of gt_condtab.
FIELD-SYMBOLS <fs_wherecond> TYPE ty_s_clause.
data:
  gt_where_clauses  type standard table of ty_s_clause
                    with default key.
gt_condtab-field = 'MATNR'.
gt_condtab-opera = 'EQ'.
gt_condtab-low = '000000000000000111'.
append  gt_condtab.
clear  gt_condtab.
call function 'RH_DYNAMIC_WHERE_BUILD'
  exporting
    dbtable         = space " can be empty
  tables
    condtab         = gt_condtab
    where_clause    = gt_where_clauses
  exceptions
    empty_condtab   = 01
    no_db_field     = 02
    unknown_db      = 03
    wrong_condition = 04.
select matnr from mara into table itab where (gt_where_clauses).

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

  • Please help - NE where condition in SELECT statement

    Dear experts,
    I am posting a section of my codes here for your review on performance tuning.
    In my second select statement, I used a "NE" where condition. I read somewhere in this forum that using "NE" where condition is not a good decision for improving codes' performance. What alternatives can I have to achieve the same purpose? May I use "NOT IN" here instead? Or do I use a LOOP for this (a rather manual way)?
    Just to let you all know that I still consider myself quite inexperienced in ABAP - please also let me know how I can better improvise my programming techniques in the posted codes here too.
    I will be most glad to provide you with further information if needed - just let me know.
    Many THANKS in advance!
    IF p_noncis = 'X'.      " Non CIS category of spend selected
        " zfi_cis_mat_grp is a bespoke table that stores all CIS MATKL
        " and it has two fields only - MANDT and MATKL
        SELECT * FROM zfi_cis_mat_grp    
        INTO TABLE gt_cis_mat_grp.
        IF gt_cis_mat_grp IS NOT INITIAL.
          SELECT ebeln
                 ebelp
               matkl
          FROM ekpo
          INTO TABLE gt_ekpo
          FOR ALL ENTRIES IN gt_cis_mat_grp
          WHERE matkl NE gt_cis_mat_grp-matkl.    " NE where condition - is this OK?
        ENDIF.
        IF gt_ekpo IS NOT INITIAL.
          IF s_sakto IS NOT INITIAL.
            SELECT ebeln
                   ebelp
                   sakto
            FROM ekkn
            INTO TABLE gt_ekkn
            FOR ALL ENTRIES IN gt_ekpo
            WHERE ebeln = gt_ekpo-ebeln AND
                  ebelp = gt_ekpo-ebelp AND
                  sakto IN s_sakto.
            IF gt_ekkn IS NOT INITIAL.
              SELECT bukrs
                     lifnr
                     belnr
                     budat
                     cpudt
                     xblnr
                     ebeln
                     ebelp
                     zfbdt
                     zterm
                     zlspr
              FROM bsik
              INTO TABLE gt_bsik
              FOR ALL ENTRIES IN gt_ekkn
              WHERE bukrs IN s_bukrs AND
                    lifnr IN s_lifnr AND
                    budat IN s_budat AND
                    cpudt IN s_cpudt AND
                    xblnr IN s_xblnr AND
                    ebeln = gt_ekkn-ebeln AND
                    ebelp = gt_ekkn-ebelp AND
                    qsskz NE ''.
            ENDIF.
          ELSE.
            SELECT bukrs
                   lifnr
                   belnr
                   budat
                   cpudt
                   xblnr
                   ebeln
                   ebelp
                   zfbdt
                   zterm
                   zlspr
            FROM bsik
            INTO TABLE gt_bsik
            FOR ALL ENTRIES IN gt_ekpo
            WHERE bukrs IN s_bukrs AND
                  lifnr IN s_lifnr AND
                  budat IN s_budat AND
                  cpudt IN s_cpudt AND
                  xblnr IN s_xblnr AND
                  ebeln = gt_ekpo-ebeln AND
                  ebelp = gt_ekpo-ebelp AND
                  qsskz NE ''.
          ENDIF.
        ENDIF.
      ELSE.      " Complete list of category of spend selected
        SELECT bukrs
               lifnr
               belnr
               budat
                  cpudt
               xblnr
               ebeln
               ebelp
               zfbdt
               zterm
               zlspr
        FROM bsik
        INTO TABLE gt_bsik
        WHERE bukrs IN s_bukrs AND
              lifnr IN s_lifnr AND
              budat IN s_budat AND
              cpudt IN s_cpudt AND
              xblnr IN s_xblnr AND
              qsskz NE ''.
      ENDIF.

    Hi,
    If you want to remove th NE option then try this way..
    SELECT bukrs
               lifnr
               belnr
               budat
                  cpudt
               xblnr
               ebeln
               ebelp
               zfbdt
               zterm
               zlspr
        FROM bsik
        INTO TABLE gt_bsik
        WHERE bukrs IN s_bukrs AND
              lifnr IN s_lifnr AND
              budat IN s_budat AND
              cpudt IN s_cpudt AND
              xblnr IN s_xblnr .
    IF SY-SUBRC EQ 0.
      Delete gt_bsik where qsskz EQ ' '.
    ENDIF.

  • Dynamis where Condition using select-options

    Hi All,
    I am trying to form a dynamic where clause using values entered on a selection screen. I want that if a new entry is added in the selection screen the where clause can also be modified dynamically without any change in coding. The only change required should be addition of select-option statement in the selection screen declaration.
    Thanks in advance.
    Anju

    refer below example
    L_COND_SYNTAX = 'd2ext_status = l_status and d2ext_date ne c_intdate and '
    & 'd2~ext_uploaded = space'.
    SELECT D1TKNUM D1SHTYP D1TPLST D1ERNAM D1ERDAT D1BFART
    D1VSART D1ROUTE D1SIGNI D1EXTI1 D1~TPBEZ
    D1DATBG D1UATBG D1TDLNR D2VBELN D2~POSNR
    D2EXT_FAILED D2EXT_COUNT D2EXT_DATE D2EXT_TIME
    D2EXT_REP_DATE D2EXT_REP_TIME
    D2EXT_RES_DATE D2EXT_RES_TIME D2~EXT_ERR_TYPE
    INTO TABLE PA_GT_SHIP_HEADER
    FROM VTTK AS D1 INNER JOIN RIOZTM_DELSTAT AS D2
    ON D1TKNUM = D2TKNUM
    WHERE
    D1~TKNUM IN S_TKNUM AND
    D1~SHTYP IN S_SHTYP AND
    AND
    <b>(L_COND_SYNTAX).</b>
    Plz note the backet in L_COND_SYNTAX.
    Reward points if helpful

  • Dynamic select query with dynamic where condition

    Hi all,
    I want to use the dynamic select query with dynamic where condition. For that I used the below code but I am getting dump when using this code.
    Please advice, if there is any other way to achieve this requirement.
    Thanks,
    Sanket Sethi
    Code***************
    PARAMETERS: p_tabnam      TYPE tabname,
                p_selfl1      TYPE edpline,
                p_value       TYPE edpline,
                p_where1      TYPE edpline .
    DATA: lt_where    TYPE TABLE OF edpline,
          lt_sel_list TYPE TABLE OF edpline,
          l_wa_name   TYPE string,
          ls_where    TYPE edpline,
          l_having    TYPE string,
          dref        TYPE REF TO data,
          itab_type   TYPE REF TO cl_abap_tabledescr,
          struct_type TYPE REF TO cl_abap_structdescr,
          elem_type   TYPE REF TO cl_abap_elemdescr,
          comp_tab    TYPE cl_abap_structdescr=>component_table,
          comp_fld    TYPE cl_abap_structdescr=>component.
    TYPES: f_count TYPE i.
    FIELD-SYMBOLS : <lt_outtab> TYPE ANY TABLE,
    *                <ls_outtab> TYPE ANY,
                    <l_fld> TYPE ANY.
    struct_type ?= cl_abap_typedescr=>describe_by_name( p_tabnam ).
    elem_type   ?= cl_abap_elemdescr=>describe_by_name( 'F_COUNT' ).
    comp_tab = struct_type->get_components( ).
    comp_fld-name = 'F_COUNT'.
    comp_fld-type = elem_type.
    APPEND comp_fld TO comp_tab.
    struct_type = cl_abap_structdescr=>create( comp_tab ).
    itab_type   = cl_abap_tabledescr=>create( struct_type ).
    l_wa_name = 'l_WA'.
    CREATE DATA dref TYPE HANDLE itab_type.
    ASSIGN dref->* TO <lt_outtab>.
    *CREATE DATA dref TYPE HANDLE struct_type.
    *ASSIGN dref->* TO <ls_outtab>.
    * Creation of the selection fields
    APPEND p_selfl1 TO lt_sel_list.
    APPEND 'COUNT(*) AS F_COUNT' TO lt_sel_list.
    ** Creation of the "where" clause
    *CONCATENATE p_selfl1 '= '' p_value ''.'
    *            INTO ls_where
    *            SEPARATED BY space.
    *APPEND ls_where TO lt_where.
    * Creation of the "where" clause
    APPEND p_where1 TO lt_where.
    * Creation of the "having" clause
    l_having = 'count(*) >= 1'.
    * THE dynamic select
    SELECT          (lt_sel_list)
           FROM     (p_tabnam)
           INTO CORRESPONDING FIELDS OF TABLE <lt_outtab>.
    *       WHERE    (lt_where).

    Hi Sanket,
    The above given logic of mine works for you, put the code in the If condition and try-
    just like below:
    IF NOT P_EBELN IS INITIAL.
    lt_where = '& = ''&'' '.
    REPLACE '&' WITH p_ebeln INTO lt_where.
    REPLACE '&' WITH field_value INTO lt_where.
    SELECT (lt_sel_list) INTO CORRESPONDING FIELDS OF TABLE <lt_outtab>
    FROM (p_tabnam)
    WHERE (lt_where).
    ENDIF.
    thanks\
    Mahesh

  • Dynamic where condition

    Hi, I'm building a procedure with 4 parameters το pass in input and I ask if it's possible to build a select inside the procedure with a dynamic where condition.
    The query with 2 conditions is the following:
    SELECT count(incident_number) FROM cs.cs_incidents_all_b
    1st condition
    WHERE (TO_DATE(incident_attribute_6,'dd-mm-yyyy hh24:mi') BETWEEN TO_DATE(p_create_date_ll,'dd-mm-yyyy hh24:mi') AND TO_DATE(p_create_date_ul,'dd-mm-yyyy hh24:mi'))
    2nd condition
    AND external_attribute_4 BETWEEN p_resolv_time_ll AND p_resolv_time_ul;
    The first condition has to be always valid, but the second one, depending on the parameters has to be satisfied or not.
    The first 2 parameters (p_create_date_ll and p_create_date_ul) are always written as a string (so the first condition always works).
    The second pair of paramenters (p_resolv_time_ll and p_resolv_time_ul) can be written as numbers (from a form), or written both as '*'
    So, If the parameters p_resolv_time_ll and p_resolv_time_ul are numbers, I should retrieve a small set of data (all the 2 where condition have to work),
    while if they are both '*' ONLY the first condition has to work. Practically, in this last case the 2nd condition is how it was commented.
    Is it possible to build something that satisfy my request ?
    Thanks in advance
    Alex

    Yes I know....
    If I use an IF THEN ELSE statement I can write 2 different queries with 2 different where conditions....but I want to avoid this solution because I have 4 pair of these parameters which can assume differents values and depending on their values I have to use different where conditions
    If I use an IF THEN ELSE statement, this means to write 16 different queries....
    I already used DECODE or CASE but they not satisfy my request....
    Anybody has others ideas ?
    Thanks

  • WHERE clause in SELECT statement

    hi experts..
    i want to give 'OR' condition in the 'where' clause of 'SELECT' statement.
    is it possible?
    for examlpe..
    IF EXIDV2 IS NOT INITIAL.
       SELECT * FROM YSDT_SHIPLOAD
                INTO TABLE IG_SHIPLOAD
                WHERE EXIDV2 = EXIDV2 AND
                             VHILM = PC1 OR PC2 OR PC3.
    ENDIF.
    i want that VHILM should be one of those three.
    how can i do this?
    thanks..

    Hi ,
    its possible,
    Select * from ysdt_shipload int table ig_shipload where exidv2 = exidv2
                                                                            AND vhilm = pc1
                                                                            OR     vhilm = pc2
                                                                             OR   vhilm = pc3.
    OR
    Select * from ysdt_shipload int table ig_shipload where exidv2 = exidv2
                        AND (vhilm = pc1 or vhilm = pc2 or vhilm = pc3).
    Regards
    Arani Bhaskar
    Edited by: arani bhaskar on Mar 16, 2009 5:14 PM

  • Checking conditions in SELECT statement

    Hi All,
    I am relative new to ABAP and I would like to ask a question about checking conditions in SELECT statement in the "WHERE" part.
    There are two checkboxes at the selection screen and each should disable one of  conditions (marked with two stars) in the SELECT mentioned below.
    My question is, whether there exists an option how to solve this problem without using solution like:
    IF checkobx1.
    SELECT (without one condition)
    ELSEIF checkbox2.
    SELECT(without other condition).
    ELSE.
    SELECT (with both conditions)
      SELECT  qprueflos qherkunft qaufnr qsa_aufnr qmatnr qwerkvorg
              qpastrterm  qpaendterm
              qverid qobjnr vobjnr AS objnr_fa vauart
        FROM qals AS q INNER JOIN vkaufk AS v
        ON qaufnr = vaufnr
        INTO CORRESPONDING FIELDS OF TABLE gt_qals
        WHERE q~prueflos IN s_pruefl
          AND q~stat35     EQ space
          AND q~werk       EQ loswk
          AND q~herkunft IN s_herk
          AND q~offennlzmk EQ 0
          AND q~offen_lzmk EQ 0
          AND q~pastrterm IN s_startt
          AND q~paendterm LE s_endt
          AND v~auart IN s_auart.    "('ZCPA', 'ZCPK', 'ZCBA').

    Hi,
    With this, I think u can directly read into WHERE clause
    IF checkbox1.
        v_where = '& BETWEEN ''&'' AND ''&'' '.
        REPLACE '&' WITH key_field INTO v_where.
        REPLACE '&' WITH field_LOW INTO v_where.
        REPLACE '&' WITH field_HIGH INTO v_where.
        CONDENSE v_where.
    ELSEIF  checkbox2.
        v_where = '& BETWEEN ''&'' AND ''&'' '.
        REPLACE '&' WITH key_field INTO v_where.
        REPLACE '&' WITH field_LOW INTO v_where.
        REPLACE '&' WITH field_HIGH INTO v_where.
        CONDENSE v_where.
    ENDIF.
    select * into corresponding fields of table ITAB
                 from (table_name)
                where (v_where).
    In this key_field is your fieldname in the where clause and field_low, field_high are range of values.
    If i write static query it looks like this
    RANGES: MATNR1 FOR MARA-MATNR.
      MATNR1-LOW = MATNR_LOW.
      MATNR1-HIGH = MATNR_HIGH.
      MATNR1-SIGN = 'I'.
      MATNR1-OPTION = 'BT'.
      APPEND MATNR1.
    select * into corresponding fields of table itab
    from mara where matnr BETWEEN 'M100' AND 'M200'.
    Hope it helps u
    thanks\
    Mahesh
    Edited by: Mahesh Reddy on Jan 30, 2009 11:23 AM

  • Issue with Dynamic WHERE condition in Cursor in FUNCTION.

    Hi All,
    I am facing an issue with cursor having dynamic WHERE condition in a function.
    Below is the FUNCTION:
    CREATE OR REPLACE FUNCTION EXCEPTION_MERGE(TABLE_NAME IN VARCHAR2, TAB_NAME IN VARCHAR2)
    RETURN VARCHAr2
    IS
    stmt_tabcols VARCHAR2(32767);
    v_columnname VARCHAR2(32767);
    CURSOR C1 IS
    SELECT 'A.'||A.COLUMN_NAME ||' = '|| 'B.'||B.COLUMN_NAME COLUMN_NAME
    FROM
    SELECT COLUMN_ID, COLUMN_NAME
    FROM USER_TAB_COLUMNS
    WHERE TABLE_NAME  = TABLE_NAME
    AND COLUMN_NAME NOT IN ('ERROR_TAB_ID','ERROR_LOAD_DATE')
    ) A,
    SELECT COLUMN_ID, COLUMN_NAME
    FROM USER_TAB_COLUMNS
    WHERE TABLE_NAME = TAB_NAME
    ) B
    WHERE A.COLUMN_ID = B.COLUMN_ID;
    BEGIN
    FOR TABCOL IN C1
    LOOP
        stmt_tabcols := stmt_tabcols ||TABCOL.COLUMN_NAME||',';
    END LOOP;
        stmt_tabcols := RTRIM(stmt_tabcols, ',');
        RETURN stmt_tabcols;
    END;
    SELECT EXCEPTION_MERGE('WC_W_TEST_FS','WC_W_TEST_FS_GBL') FROM DUAL;It throws, below error:
    ORA-06502 : PL/SQL : Numeric or value error : character string buffer too smallIf I REPLACE TABLE_NAME and TAB_NAME with hard coded values , it works fine. Can somebody look at the code and let me know the issue.
    Edited by: ace_friends22 on Sep 9, 2012 1:08 PM

    Etbin neatly demonstrating the value of posting code in a manner which makes it easy to read.
    It's obviously an naming/scoping issue. Faced with a join like this:
    where table_name = table_namethe engine looks for something called table_name in the current scope. It finds it, a column on USER_TAB_COLUMNS and applies it to both sides of the filter. It has no way of knowing that there is also a parameter called TABLE_NAME, because that is outside its current scope. Consequently the query will join every table in your schema regardless of what values you pass, and that's why you blow the buffer.
    Takw etbin's advice and name your parameter with a prefix:
    where table_name = p_table_nameThis isn't a column in USER_TAB_COLUMNS which will force the engine to look in the next scope up, which in your case is the function, where it will find your parameter and so generate a query for the passed values only.
    Cheers, APC
    Edited by: APC on Sep 9, 2012 8:03 AM

  • Single quote in dynamic where condition

    BAPI_STUDENT_IDENTIFIC_ADD has a field called IDENTIFICATIONNUMBER.  This field is later used in a dynamic where condition and causes a short dump when it contains a single quote.  I will change the program that calls the BAPI to check for single quotes, but is there anything else I need to check for to ensure a correct where condition?
    Thanks,
    Dan

    Hi Dan,
    The best way to ensure correctness of syntax of a dynamic where condition is:
    Run the program in debugging mode, Get the Query that is generated dynamically and write it to some other ABAP program and perform syntax check.
    This will help you to remove all the syntax errors.
    Regards,
    Darshil

  • 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

  • Dynamic where condition - Problem

    Hi All,
    I have a problem in a select statement where I use a dynamic condition.It gives me a dump
    SAPSQL_WHERE_PARENTHESES
    CX_SY_DYNAMIC_OSQL_SYNTAX.
    Please find the below code which I implemented.
      *SELECT * FROM ZXR5*
               WHERE (SOURCE).
    The value of SOURCE is 'MATNR EQ '000000000000200066'.
    When I checked the help, it is advised to use a CATCH exception which I did as below:
    TRY.
      *SELECT * FROM ZXR5*
               WHERE (SOURCE).
      CATCH cx_sy_dynamic_osql_error.
        MESSAGE `Wrong WHERE condition!` TYPE 'I'.
    ENDTRY.
    Now, the select statement gives an exception and is not executing to fetch the values.
    I need some input on the above issue. Any help on this would be greatly appreciated.
    Regards,
    Rajmohamed.M

    Hi Vijay,
    I tried the same and all the time, the select statement gives me an exception and wont proceed.
      TRY.
        SELECT * FROM ZXR5
                 INTO TABLE <L_ZXR5>
                 WHERE (SOURCE).
        CATCH cx_sy_dynamic_osql_error.
        MESSAGE `Wrong WHERE condition!` TYPE 'I'.
      ENDTRY.
    Regards,
    Raj

  • Dump using Dynamic WHERE condition (EXISTS + subquery)

    Hi experts,
    I want to use dynamic WHERE here, but I got a problem when using EXISTS + subuery.
    Here is my code snippet:
    And the actual SQL sentence should be like this:
    When I execute this program, an exception CX_SY_DYNAMIC_OSQL_SEMANTICS is raised and the program dumps.
    Dump Information:
    Short text
       A dynamically specified column name is unknown.
    Error analysis
        An exception occurred that is explained in detail below.
        The exception, which is assigned to class 'CX_SY_DYNAMIC_OSQL_SEMANTICS', was
         not caught in
        procedure "FRM_GET_ALL_PROD_ORDERS" "(FORM)", nor was it propagated by a
         RAISING clause.
        Since the caller of the procedure could not have anticipated that the
        exception would occur, the current program is terminated.
        The reason for the exception is:
        An Open SQL clause was specified dynamically. The contained field name
        "EXISTS" does not exist in any of the database tables from the FROM clause.
    I'm confused because the plain text of my WHERE condition looks good. Could you tell me why I encountered this problem?
    Many thanks,
    Shelwin

    Hi Shelwin !
    Testing your code i dont have dump, using this:
    REPORT z_test MESSAGE-ID z_test_msgs.
    DATA: lt_where_tab1 TYPE STANDARD TABLE OF edpline WITH HEADER LINE,
          lt_where_tab2 TYPE STANDARD TABLE OF edpline WITH HEADER LINE,
          gt_detail     TYPE STANDARD TABLE OF caufv WITH HEADER LINE.
    CONCATENATE 'EXISTS (SELECT J_1~STAT FROM JEST AS J_1 INNER JOIN TJ02T AS T_1'
                'ON J_1~STAT EQ T_1~ISTAT'
                'WHERE J_1~OBJNR EQ CAUFV~OBJNR'
                'AND J_1~INACT EQ '' '''
                'AND T_1~SPRAS EQ ''E'''
                'AND T_1~TXT04 EQ ''CRTD'')'
                INTO lt_where_tab1.
    CONCATENATE 'NOT EXISTS (SELECT J_2~STAT FROM JEST AS J_2 INNER JOIN TJ02T AS T_2'
                'ON J_2~STAT EQ T_2~ISTAT'
                'WHERE J_2~OBJNR EQ CAUFV~OBJNR'
                'AND J_2~INACT EQ '' '''
                'AND T_2~SPRAS EQ ''E'''
                'AND T_1~TXT04 IN (''TECO'' , ''DLFL'', ''DLV'', ''CLSD''))'
                INTO lt_where_tab2.
    SELECT aufnr ftrms gltrs
      FROM caufv
      INTO CORRESPONDING FIELDS OF TABLE gt_detail
    WHERE (lt_where_tab1)
       AND (lt_where_tab2).
    BREAK-POINT.
    Regards,
    Edited for error on image.

  • Conditional Mysql select statement

    HI Folks
    can anyone point me in the right direction with a MySQL statement.
    I will try to layout my thinking here:
    I have a form with three inputs area, name and search. I am trying to write a Mysql select statement that selects records from a single table if they match the criteria. Easy for two variables but I'm lost after that.
    1. The form includes these three inputs:
    area - drop down menu (Any as default)
    name - drop down menu (Any as default)
    search box - text area (Blank as default)
    2. The form submits to itself leaving me with these three variables
    $search=$_GET['search']
    $area=$_GET['area']
    $name=$_GET['area']
    SELECT * FROM database WHERE database.description LIKE '%$search%' AND database.area LIKE '$area' AND database.name LIKE '$name'
    3. This is where I get confused. How do I get the SQL to Select everything correctly. I have tried using PHP if/else code to fix it but I end up running around in circles with six different Select statements and haven't yet got that to work.
    So I have come to the conclusion that there must be an easier way.  I see search forms with dozens of  search criteria on websites every day an d I only have 3 - so it can't be this complicated. Right?
    I know I need to start from the beginning again but can anyone let me know how to approach it before I begin?
    Cheers
    Dave

    Typically, I would build the where clause dynamically, based upon the values in your form. If the form field contains 'Any', leave it out of the where clause. So you can test each field value and either append or not to the end of the where clause.

  • Using 'Greater than/less than' in dynamic where condition

    Hi Guys,
    I am trying to use a logical expression with GT/LT condition in dynamic where, butits giving dump  "expression worngly parenthesised' . My query runs perfectly fine when the logical expression does not have 'GT/LT' . Is there a different way of using GT/LT in dynamic where or is it not posiible at all?? Please find my query below -
    IF vendor IS NOT INITIAL.
          i_where_clause = 'a~lifnr = vendor'.
          APPEND i_where_clause.
          CLEAR i_where_clause.
        ENDIF.
        IF to_date IS NOT INITIAL.
          i_where_clause = 'a~zzearliestdep LT to_date'.
          APPEND i_where_clause.
          CLEAR i_where_clause.
        ENDIF.
        IF from_date IS NOT INITIAL.
          i_where_clause = 'a~zzlatestdep GT from_date'.
          APPEND i_where_clause.
          CLEAR i_where_clause.
        ENDIF.
      SELECT aebeln alifnr azzearliestdep azzlatestdep
                bebelp belikz b~kzabs
           INTO CORRESPONDING FIELDS OF TABLE i_podetails
           UP TO max_hits ROWS
           FROM ( ekko AS a INNER JOIN ekpo AS b
                   ON aebeln = bebeln )
           WHERE a~ebeln = ebeln AND
                (i_where_clause).

    Hi,
    Try this -
    IF vendor IS NOT INITIAL.
      i_where_clause = 'a~lifnr = vendor'.
      APPEND i_where_clause.
    ENDIF.
    IF to_date IS NOT INITIAL.
      if i_where_cause[] is not initial.
        i_where_clause = 'AND'
        append i_where_cause.
      endif.
      i_where_clause = 'a~zzearliestdep LT to_date'.
      APPEND i_where_clause.
    ENDIF.
    IF from_date IS NOT INITIAL.
      if i_where_cause[] is not initial.
        i_where_clause = 'AND'
        append i_where_cause.
      endif.
      i_where_clause = 'a~zzlatestdep GT from_date'.
      APPEND i_where_clause.
      CLEAR i_where_clause.
    ENDIF.
    Cheers.
    \[removed by moderator\]
    Edited by: Jan Stallkamp on Jun 26, 2008 1:08 PM

Maybe you are looking for

  • User exit message problem

    hi all i am giving an error message in a user exit the error message comes as a pop up and moving ahead takes me out of the transaction i seriously donot want this. even worning messages are coming as success ones is there a way out for giving error

  • Apache not working  with FMS 4.5 on Windows 2008

    FMS 4.5.1 Windows 2008 SP2 (64bit). I've searched everywhere for a possible solution to this, so my apologies if this has been asked before and my thanks for any potential help that anyone can offer to help resolve this. We are deploying Adobe Flash

  • Relation between the Height in Pixels and Font Size?

    Hi All, I wanted to find the relation between the Font Size and the Pixels that it will occupy. What i want to do is i have an Icon and a Label. The icon size i need to resize to the Half the height of the Label. But the font Size does not reflect th

  • Info Needed

    Hi, I would like to know about the function which clears the screen while running a program

  • OwnCloud and KeePass Integration Advice

    I'm relatively new to Arch Linux, though I've been using Ubuntu and Fedora for a number of years. I'm wanting to set up an ownCloud server on my Arch Linux desktop with a classic LAMP stack, but this will be my first foray into server setup. The syst