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: 

Dynamic Select Statements

Former Member
0 Kudos

Hi there,

Is there a way to have dynamic select statements such that

the Table name can be dynamically given and field names for selection criterion should be dynamically given in the select statment.

To be more precise, I have set of tables for which I need to fetch data from the database using a single select statment.

If any of you have a solution .please revert ASAP.

Many thanks

ugesh

3 REPLIES 3

Former Member
0 Kudos

Hi, Ugesh.

Extract from ABAP help about SELECT statement:

FROM clause:

... FROM (dbtabname)

Effect

Selects the data from a database table or view. The name is specified at runtime as the contents of the field dbtabname. The database table must be recognized in the ABAP Dictionary.

SELECT clause:

SELECT (itab)

Effect

Works like SELECT s1 ... sn, if the internal table itab contains the list s1 ... sn as ABAP source code, and works like SELECT *, if itab is empty. The internal table itab may only contain one field, which must have type C and not be longer than 72 characters. You must specify itab in parentheses. Do not include spaces between the parentheses and the table name.

Regards,

Maxim.

Former Member
0 Kudos

Hi,

See this sample code this will satisfy ur requirement

For dynamic WHERE condition try this:

DATA: BEGIN OF lt_cond OCCURS 0 ,

cond_line(60) TYPE c ,

END OF lt_cond .

REFRESH lt_cond .

CONCATENATE fieldname 'EQ' enteredvalue '.'

INTO lt_cond-cond_line

SEPARATED BY space .

APPEND lt_cond .

SELECT SINGLE key INTO varkey

FROM (tablename)

WHERE lt_cond .

<b>OR</b>

This is how you need to modify your code.

DATA: BEGIN OF lt_cond OCCURS 0 ,

cond_line(60) TYPE c ,

END OF lt_cond .

IF glacct-low IS INITIAL.

searchedfld = 'GLACCT'.

enteredvalue = glacct-low.

ELSEIF custno-low IS INITIAL.

searchedfld = 'CUSTNO'.

enteredvalue = custno-low.

ELSE.

EXIT.

ENDIF.

SELECT catalogname fld INTO (tablename,fieldname)

FROM zcatfld

WHERE searchedfld EQ searchfld.

ENDSELECT.

REFRESH lt_cond.

CLEAR lt_cond.

CONCATENATE fieldname

'EQ'

enteredvalue

'.'

INTO lt_cond-cond_line SEPARATED BY space .

APPEND lt_cond .

CLEAR lt_cond.

SELECT key INTO varkey

FROM (tablename)

WHERE (lt_cond).

ENDSELECT.

Make sure that the field name and the table name are in uppercase.

Try any one of these depending on ur requirement.

Hope this helps...

Thanks & Regards,

Judith

Former Member
0 Kudos

Hi Ugesh,

Have u tried this out.

U have marked as problem solved??????

Thanks & Regards,

Judith.