Skip to Content
author's profile photo Former Member
Former Member

variable in IN operator

Dear All,

I would like to ask if it is possible to use variable inside WHERE-IN clause.

Say:

SELECT id name INTO TABLE itab FROM table WHERE id IN ('A','B','C').

I would like 'A','B','C' to be in a variable e.g. mytext so that it would be:

SELECT id name INTO TABLE itab FROM table WHERE id IN (mytext).

Thank you very much.

Points will be awarded.

Regards,

Rayel

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

7 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Mar 26, 2009 at 07:10 AM

    Hi Rayel,

    Yes of course. Try this way:

    data:
      w_var(10) type c.
    
    Ranges:
      r_var1 for w_var.
    
    r_var1-sign = 'I'.
    r_var1-option = 'EQ'.
    r_var1-low = 'A'.
    append r_var1.
    
    r_var1-sign = 'I'.
    r_var1-option = 'EQ'.
    r_var1-low = 'B'.
    append r_var1.
    
    r_var1-sign = 'I'.
    r_var1-option = 'EQ'.
    r_var1-low = 'C'.
    append r_var1.
    
    SELECT id name INTO TABLE itab FROM table WHERE id IN r_var1.

    This will solve.

    Regards,

    Swapna.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Mar 26, 2009 at 07:10 AM

    Hi,

    Instead you can create range and pass to select query.

    Sample Code:

    DATA: l_count_bsid TYPE tbmaxsel,

    l_bal_count TYPE tbmaxsel,

    lt_blart TYPE lvc_t_rngs,

    ls_blart TYPE lvc_s_rngs.

    • Creating the Value Range for Document type

    ls_blart-sign = 'I'.

    ls_blart-option = 'EQ'.

    ls_blart-low = 'Value1'.

    APPEND ls_blart TO lt_blart.

    CLEAR ls_blart.

    ls_blart-sign = 'I'.

    ls_blart-option = 'EQ'.

    ls_blart-low = 'Value2'.

    APPEND ls_blart TO lt_blart.

    CLEAR ls_blart.

    • Get Accounting: Secondary Index for Customers data(BKPF table)

    SELECT * FROM bsid

    UP TO p_no_re ROWS

    INTO TABLE it_bsid

    WHERE bukrs IN s_bukrs AND

    gjahr IN s_gjahr AND

    belnr IN s_belnr AND

    blart IN lt_blart.

    You can add as many as you want.

    Regards,

    Prashant.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Mar 26, 2009 at 07:18 AM

    Hi,

    You can create a range of these three values like:

    
    data: r_value for table name-field name(which of character type length 1).
    
    In Initialization Event you can fill this range like:
    
    r_value-low = 'A'.
    
    r_value-sign = BT.
    
    r_value-high = 'C'.
    
    Append r_value. 
    
    And Then in your Select Query,
    
    Select Field from Table name into Internal Table name
    where id IN R_value(Range name).
    
    

    Hope it helps

    Regards

    Mansi

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Mar 26, 2009 at 07:10 AM

    absolutely yes. in that case u have to declare the variable as SELECT-OPTIONS in the SELECTION-SCREEN event,

    Eg.

    SELECTION-SCREEN BEGIN OF BLOCK BLOCK1.

    SELECT-OPTIONS: v_input_var FOR VBAP-VBELN.

    SELECTION-SCREEN END OF BLOCK BLOCK1.

    START-OF-SELECTION.

    SELECT VBELN

    POSNR

    FROM VBAP INTO TABLE IT_TAB

    WHERE VBELN IN v_input_var.

    Regards,

    Anirban.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Mar 26, 2009 at 07:11 AM

    IN class only accept the Ranges or tables.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Mar 26, 2009 at 08:08 AM

    Hi,

    You can use select-options if that is feasible, or else you have to use ranges or tables.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Mar 26, 2009 at 08:12 AM

    Hi Rayel

    Going by the basic definition of IN clause it hold the result true for rows where the value matches with them.However to specify vaiable instead of actual values may not work as it evaluates values.

    An alternative solution to this is in declaration of a macro instead of a variable before the select query is

    written.one can use macros to replace any set of repetitive statements that check or modify values of variables.the value of a macro definition is substituted at run time of program.

    Take alook at this to understand macros in abap -http://www.builderau.com.au/news/soa/Unleash-the-power-of-macros-in-ABAP/0,339028227,320276178,00.htm

    Regards

    Amarjot

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.