Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

variable in IN operator

Former Member
0 Kudos

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

1 ACCEPTED SOLUTION

Former Member
0 Kudos

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.

8 REPLIES 8

Former Member
0 Kudos

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.

0 Kudos

Hi Swapna,

Thanks. This is exactly what I needed and it worked!

Thanks to everyone who responded.

Points awarded.

Regards,

Rayel

Former Member
0 Kudos

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.

Former Member
0 Kudos

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.

Former Member
0 Kudos

IN class only accept the Ranges or tables.

Former Member
0 Kudos

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

Former Member
0 Kudos

Hi,

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

Former Member
0 Kudos

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