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 SQL query

milascon_daniel
Participant
0 Kudos

Hi,

can anyone tell me if it's possible to run a query that change during execution? For example if radiobutton1 is selected i want to select from table1 otherwise from table2;

A large piece of code fits with both queryies ... so i don't want to use twice that part of code.

Thank you.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

try:

data: g_tabnam type string.

g_tabnam = 'csks'.

select * from (g_tabnam). " do not remove brackets

5 REPLIES 5

former_member182354
Contributor
0 Kudos

Yes thats possible...use radio-buttons and code in respective subroutines to trigger them according to your radio-button selection.

Raghav

Former Member
0 Kudos

Hi,

Yes, you can do like this..

if rg1 = 'X'.

SELECT * FROM TABLE TAB1

ELSE.

SELECT * FROM TABLE TAB2.

ENDIF.

Rgds,

Former Member
0 Kudos

REPORT z_dynamic.

TYPE-POOLS : abap.

FIELD-SYMBOLS: <dyn_table> TYPE STANDARD TABLE,
               <dyn_wa>,
               <dyn_field>.

DATA: dy_table TYPE REF TO data,
      dy_line  TYPE REF TO data,
      xfc TYPE lvc_s_fcat,
      ifc TYPE lvc_t_fcat.




SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME.
PARAMETERS: p_table(30) TYPE c DEFAULT 'T001'.
SELECTION-SCREEN END OF BLOCK b1.

START-OF-SELECTION.

  PERFORM get_structure.
  PERFORM create_dynamic_itab.
  PERFORM get_data.
  PERFORM write_out.


*&---------------------------------------------------------------------*
*&      Form  get_structure
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM get_structure.

  DATA : idetails TYPE abap_compdescr_tab,
         xdetails TYPE abap_compdescr.

  DATA : ref_table_des TYPE REF TO cl_abap_structdescr.

* Get the structure of the table.
  ref_table_des ?=
      cl_abap_typedescr=>describe_by_name( p_table ).
  idetails[] = ref_table_des->components[].

  LOOP AT idetails INTO xdetails.
    CLEAR xfc.
    xfc-fieldname = xdetails-name .
    xfc-datatype = xdetails-type_kind.
    xfc-inttype = xdetails-type_kind.
    xfc-intlen = xdetails-length.
    xfc-decimals = xdetails-decimals.
    APPEND xfc TO ifc.
  ENDLOOP.

ENDFORM.                    "get_structure

*&---------------------------------------------------------------------*
*&      Form  create_dynamic_itab
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM create_dynamic_itab.

* Create dynamic internal table and assign to FS
  CALL METHOD cl_alv_table_create=>create_dynamic_table
    EXPORTING
      it_fieldcatalog = ifc
    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.                    "create_dynamic_itab



*&---------------------------------------------------------------------*
*&      Form  get_data
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM get_data.

* Select Data from table.
  SELECT * INTO TABLE <dyn_table>
             FROM (p_table).

ENDFORM.                    "get_data

*&---------------------------------------------------------------------*
*&      Form  write_out
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM write_out.
* 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.

ENDFORM.                    "write_out

Former Member
0 Kudos

try:

data: g_tabnam type string.

g_tabnam = 'csks'.

select * from (g_tabnam). " do not remove brackets

Former Member
0 Kudos

Hi

Yes it can:

DATA: T_COND(100) OCCURS 0.

DATA: WA TYPE REF TO DATA.
FIELD-SYMBOLS:  <WA> TYPE ANY.

CREATE DATA WA TYPE (V_TABLE).
ASSIGN WA->* TO <WA>.

CASE V_TABLE.
  WHEN ....
   T_COND = ........
ENDCASE.

SELECT * FROM (V_TABLE) INTO <WA> WHERE (T_COND).
  
ENDSELECT.

T_COND is an internal table where u can insert the WHERE condition, but the SELECT-OPTIONS can't be loaded in that table.

Max