Application Development and Automation 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: 
Read only

Dynamic select

Former Member
0 Likes
767

Hello all,

I have a requirement where a table is created dynamically and I have to put a select query on that,

How do I do that?

T

8 REPLIES 8
Read only

Former Member
0 Likes
735

This message was moderated.

Read only

former_member632729
Contributor
0 Likes
735

Hi,

Dynamic concept

Go through the Link:[http://www.susanto.id.au/papers/DynOpenSQL.asp]

For dynamic table name to select statement


REPORT demo_select_dynamic_database . 

DATA wa TYPE scarr. 
DATA name(10) TYPE c VALUE 'SCARR'. 

SELECT * 
INTO wa 
FROM (name) CLIENT SPECIFIED 
WHERE mandt = '000'. 
WRITE: / wa-carrid, wa-carrname. 
ENDSELECT. 

*----- 

For dynamic field list 

REPORT demo_select_dynamic_columns . 

DATA: itab TYPE STANDARD TABLE OF spfli, 
            wa LIKE LINE OF itab. 
DATA: line(72) TYPE c, 

list LIKE TABLE OF line(72). 

line = ' CITYFROM CITYTO '. 

APPEND line TO list. 

SELECT DISTINCT (list) 
INTO CORRESPONDING FIELDS OF TABLE itab 
FROM spfli. 

IF sy-subrc EQ 0. 

LOOP AT itab INTO wa. 
WRITE: / wa-cityfrom, wa-cityto. 
ENDLOOP. 

ENDIF. 

Read only

keerthy_k
Product and Topic Expert
Product and Topic Expert
0 Likes
735

Hi ,

Pls see the code below..

DATA: l_tabnme TYPE tabname VALUE 'SPFLI',

itab TYPE TABLE OF spfli.

SELECT * FROM (l_tabnme) INTO TABLE itab

WHERE carrid = 'AA'.

Hope this will help u..

Keerthi.

Read only

Former Member
0 Likes
735

select query is for database table....not for dynamically created internal table.....please specify your query...

Arunima

Read only

faisalatsap
Active Contributor
0 Likes
735

HI,

Please test my [Sample Code|] Hope this will solve out your problem,

Please Reply if else Requirement,

Best Regards,

Faisal

Read only

Former Member
0 Likes
735

This message was moderated.

Read only

I355602
Product and Topic Expert
Product and Topic Expert
0 Likes
735

This message was moderated.

Read only

matt
Active Contributor
0 Likes
735

You've not been clear whether your table is a transparent table or an internal table. But it's reasonable to assume that it's a dynamic internal table.

First of all, you need to be able to read the fields of your table.

DATA: lp_data TYPE REF TO DATA.

FIELD-SYMBOLS: <my_wa> TYPE ANY,
              <fld1> TYPE ...
...
CREATE DATA lp_data LIKE LINE OF <my_tab>.
ASSIGN lp_data->* TO <my_wa>.

ASSIGN COMPONENT 'fieldname' OF STRUCTURE <my_wa> TO <fld1>.

Now, you can't use

LOOP AT <mytab> INTO <mywa> WHERE <fld1> EQ some_value.
"  Do stuff
ENDLOOP.

But, you can use:

LOOP AT <my_tab> INTO <my_wa>.
  CHECK <fld1> EQ some_value.
" Do stuff
ENDLOOP.

You can READ the table like this:

READ TABLE <my_tab> INTO <my_wa> WITH TABLE KEY ('fieldname') = some_value.

matt