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 itab with Dynamic Where clause

Former Member
0 Kudos

Hi, Dear All,

Can someone provide a code extract for Dynamic where clause, i had already done with dynamic itab for a given set of fields, and i need to add where clause dynamically for a given field for a given range of values.

select (i_fields) into table <dyn_table>

from (p_table)

where (v_where).

In the above except the where clause, everything is done. Please help me.

with best regards

Mahesh

8 REPLIES 8

Former Member
0 Kudos

Mahesh,

check these fm's

CLDY_DYN_SEL_WHERETAB_CREATE

DYNSQL_GENERATE_WHERE_CLAUSE

RH_DYNAMIC_WHERE_BUILD

Former Member
0 Kudos

Hi

Use it like this:

constants: c_var(15) type c value ' IN S_RANGE'.(Here specify your dynamic filed name or condition)

data: v_where type string.

concatenate p_field c_var into v_where.

Regards

Preeti

Former Member
0 Kudos

Hi Mahesh,

actually you only have to concatenate your where clause into

v_where the same way you would code it, if it's not dynamically.

Regards Henner

Former Member
0 Kudos

Hi, Preeti,

I tried the same way as you suggested, but it is either leading into dump or exception clause.

CONCATENATE p_field c_var INTO v_where.

thank u

0 Kudos

Hi..

You have to Give like this

CONCATENATE p_field c_var INTO v_where SEPERATED BY SPACE.

Otherwise it will not add the SPACE between fieldnames that will generate the Runtime error.

You can check the value of V_WHERE in debugging to know whether it is proper.

<b>Reward if Helpful</b>

Former Member
0 Kudos

Hii

Here is some code:


constants:       c_var(15) type c value ' IN S_RANGE'.
parameters:
   p_table type dd02l-tabname, " Parameter to capture table name.
   p_field(10) type c. " Parameter to capture field name.
data: v_where type string.
concatenate p_field c_var into v_where.
try.
      select * into corresponding fields of table <dyn_table>
                   from (p_table) where (v_where) .

      if sy-dbcnt = 0.

        write : text-t02.

      endif.

*  Exception Catching.

    catch cx_root into o_field.
      text = o_field->get_text( ).

It works perfectly for me...

Former Member
0 Kudos

Preeti,

I was also with the same code, but unluckily it is getting error, i ran the program in debug mode,,it is leading into exception only when reaches the Select with where clause. I am getting the following exception.

'A value List has a format containing errors.'

with regrds

Former Member
0 Kudos

Hi,

here is the code extract for your reference.Pl. correct me.

with regards

REPORT Z_DYN_ITAB .

TYPE-POOLS: SLIS.

DATA:NAME(100) TYPE C.

TYPES: BEGIN OF ITAB_TYPE,

WORD(20),

END OF ITAB_TYPE.

DATA: ITAB TYPE STANDARD TABLE OF ITAB_TYPE WITH HEADER LINE.

DATA: vg_fields(255) TYPE c,

i_fields LIKE TABLE OF vg_fields.

DATA: it_fcat TYPE slis_t_fieldcat_alv,

is_fcat LIKE LINE OF it_fcat,

ls_layout TYPE slis_layout_alv.

DATA: it_fieldcat TYPE lvc_t_fcat,

is_fieldcat LIKE LINE OF it_fieldcat.

field-symbols: <dyn_table> type standard table,

<dyn_wa>,

<dyn_field>.

data: dy_table type ref to data.

data: dy_line type ref to data,

xfc type lvc_s_fcat.

DATA: v_where TYPE string, " Variable for storing where clause.

v_dynamic(18) TYPE c, "variable to store select option datatype

o_field TYPE REF TO cx_root," object to catch exception

text TYPE string. "string variable to store exception text.

CONSTANTS: c_var(15) TYPE c VALUE ' IN S_RANGE'.

selection-screen begin of block b1 with frame.

parameters: p_table(30) type c default 'T001',

p_name(100) type c,

p_field(10) TYPE c. " Parameter to capture field name.

SELECT-OPTIONS: s_range FOR v_dynamic. " Select-option for range.

selection-screen end of block b1.

start-of-selection.

NAME = p_name.

SPLIT NAME AT ',' INTO TABLE ITAB.

LOOP AT ITAB.

is_fcat-fieldname = itab-word.

is_fcat-tabname = p_table.

APPEND is_fcat to it_fcat.

ENDLOOP.

LOOP AT it_fcat INTO is_fcat.

is_fieldcat-fieldname = is_fcat-fieldname.

is_fieldcat-tabname = is_fcat-tabname.

APPEND is_fieldcat TO it_fieldcat.

CONCATENATE is_fieldcat-tabname is_fieldcat-fieldname INTO

vg_fields SEPARATED BY '~'.

APPEND vg_fields TO i_fields.

ENDLOOP.

perform create_dynamic_itab.

perform get_data.

  • Create dynamic internal table and assign to FS

form create_dynamic_itab.

call method cl_alv_table_create=>create_dynamic_table

exporting

it_fieldcatalog = it_fieldcat

importing

ep_table = dy_table.

assign dy_table->* to <dyn_table>.

  • Create dynamic work area and assign to FS

create data dy_line like line of <dyn_table>.

assign dy_line->* to <dyn_wa>.

endform.

form get_data.

  • Select Data from table.

CONCATENATE p_field c_var INTO v_where.

TRY.

select (i_fields) into table <dyn_table>

from (p_table)

where (v_where).

if sy-dbcnt = 0.

write : text-t02.

endif.

*Write out data from table.

Loop at <dyn_table> into <dyn_wa>.

do.

assign component sy-index of structure <dyn_wa> to <dyn_field>.

if sy-subrc <> 0.

exit.

endif.

if sy-index = 1.

write:/ <dyn_field>.

else.

write: <dyn_field>.

endif.

enddo.

endloop.

  • Exception Catching.

CATCH cx_root INTO o_field.

text = o_field->get_text( ).

  • Calling Function to give information message regarding Exception

CALL FUNCTION 'POPUP_TO_INFORM'

EXPORTING

titel = text-t03

txt1 = text

txt2 = text-t04.

  • TXT3 = ' '

  • TXT4 = ' '

LEAVE TO LIST-PROCESSING.

ENDTRY.

endform.