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
861

Hi All

I need to pass internal tables to a select query dynamically. The database table from which i am selecting remains the same but the internal tables are subjected to change. Since I am using for all entries addition, the interpreter refuses to recognize the structure of the internal table on which i am using for all entries.

The code i tried to work with is as follows, but it throws a syntax error, saying "the structure of <fs_tab2> is unknown, there is no component matwa".

form sel_kotd001 using value(fp_tab1)

value(fp_tab2).

field-symbols:<fs_tab1> type standard table,

<fs_tab2> type standard table.

assign fp_tab1 to <fs_tab1>.

assign fp_tab2 to <fs_tab2>.

SELECT knumh

matwa

FROM kotd001

INTO TABLE <fs_tab1>

FOR ALL ENTRIES IN <fs_tab2>

WHERE matwa EQ <fs_tab2>-matwa.

endform.

any help in this regard will be appreciated

thanks

Sudhir

7 REPLIES 7
Read only

Former Member
0 Likes
827

Modify your statement like this

SELECT knumh

matwa

FROM kotd001

INTO <i><b>corresponding fields of</b></i> TABLE <fs_tab1>

FOR ALL ENTRIES IN <fs_tab2>

WHERE matwa EQ <fs_tab2>-matwa.

regards,

Ravi

note - Please mark all the helpful answers

Read only

0 Likes
827

Hi ravi

It does not work, the problem is with the recognition of structure of field symbol <fs_tab2> . . . . . it says "The specified type has no structure and therefore no component called MATWA" . . . .

Read only

0 Likes
827

When you use CORRESPONDING FIELDS, the system does not check for fields at design time. I guess you have not use CORRESPONDING clause.

Regards,

Ravi

Note - Please mark all the helpful answers

Read only

Former Member
0 Likes
827

Hi Sudhir,

If iam not wrong , you want to use the same form for multiple purpose.

You can use this code

data: p_table(10),

p_intotable(4),

p_foralltab(4).

p_table = kotd001

p_intotable = itab say for example for into table

p_foralltab = jtab say for example of for all entries

SELECT knumh

matwa

FROM (p_table) into table (p_intotable) for all entries in (p_foralltab).

you can use this way.

Reaward points if it is helpful.

Reagrds,

Kiran I

Read only

Laxmana_Appana_
Active Contributor
0 Likes
827

Hi,

Check this :


PARAMETERS    :  p_tab LIKE dd03l-tabname,
                 p_opt LIKE rfc_db_opt-text.

FIELD-SYMBOLS : <lt_data> TYPE table. 

SELECT * FROM (p_tab)
          CLIENT SPECIFIED
   INTO CORRESPONDING FIELDS OF TABLE <lt_data>
          UP TO p_rows ROWS
    WHERE mandt = p_mandt
                 AND (p_opt).

Regards

Appana

Read only

jayanthi_jayaraman
Active Contributor
0 Likes
827

Hi,

Just try

assign <b>(fp_tab1)</b> to <fs_tab1>.

assign <b>(fp_tab2)</b> to <fs_tab2>.

Read only

Former Member
0 Likes
827

<b>you can check this code for dynamic internal table create</b>

  • ======

REPORT Z_DOWNLOAD_UPLOAD_ANY_TABLE .

  • ----------------------------------------------------------------------

  • Program for

  • ----------------------------------------------------------------------

  • 1. Downloading Data of any DB table to a tab delimited ASCII file

*

  • ----------------------------------------------------------------------

  • 2. Checking if a tab delimited ASCII file has the structure of a

  • DB table and showing its contents

  • ----------------------------------------------------------------------

  • 3. Uploading a tab delimited ASCII file to a DB table with the same

  • structure

  • ----------------------------------------------------------------------

  • 4. Showing the data of any DB table

*

  • ======================================================================

  • ======================================================================

  • DATA DECLARATIONS

  • ======================================================================

TYPES : DATA_OBJECT TYPE REF TO DATA.

DATA : MITAB TYPE REF TO DATA .

TYPE-POOLS : SLIS .

DATA : IT_FIELDCAT TYPE STANDARD TABLE OF slis_fieldcat_alv

WITH HEADER LINE .

DATA : IT_FIELDCATALOG Type LVC_T_FCAT .

DATA : WA_FIELDCATALOG Type LVC_S_FCAT .

DATA : I_STRUCTURE_NAME LIKE DD02L-TABNAME .

DATA : I_CALLBACK_PROGRAM LIKE SY-REPID .

DATA : DYN_LINE TYPE DATA_OBJECT .

FIELD-SYMBOLS : <fs_ITAB> TYPE STANDARD TABLE .

DATA : TABLE_NAME_IS_VALID TYPE C .

DATA : DYNAMIC_IT_INSTANTIATED TYPE C .

CONSTANTS BUTTONSELECTED TYPE C VALUE 'X' .

  • ======================================================================

  • SELECTION SCREEN DEFAULT

  • ======================================================================

selection-screen begin of line.

SELECTION-SCREEN COMMENT 5(29) T_tabl.

PARAMETERS : MTABLE_N LIKE RSRD1-TBMA_VAL

mATCHCODE OBJECT DD_DBTB_16 OBLIGATORY .

selection-screen end of line.

selection-screen begin of line.

SELECTION-SCREEN COMMENT 5(29) T_file.

PARAMETERS : MFILENAM LIKE RLGRAP-FILENAME .

selection-screen end of line.

selection-screen begin of line.

SELECTION-SCREEN COMMENT 5(29) T_down.

PARAMETERS : P_DOWNLD RADIOBUTTON GROUP GRP1

user-command M_UCOMM .

selection-screen end of line.

selection-screen begin of line.

SELECTION-SCREEN COMMENT 5(29) T_chkf.

PARAMETERS : P_chkfil RADIOBUTTON GROUP GRP1 .

selection-screen end of line.

selection-screen begin of line.

SELECTION-SCREEN COMMENT 5(29) T_upld.

PARAMETERS : P_UPLOAD RADIOBUTTON GROUP GRP1 .

selection-screen end of line.

selection-screen begin of line.

SELECTION-SCREEN COMMENT 5(29) T_show.

PARAMETERS : P_show_t RADIOBUTTON GROUP GRP1 .

selection-screen end of line.

  • ======================================================================

  • AT SELECTION SCREEN OUTPUT

  • ======================================================================

AT SELECTION-SCREEN OUTPUT .

PERFORM CHECK_FILENAME .

  • ======================================================================

  • AT SELECTION SCREEN ON VALUE REQUEST FOR FILENAME

  • ======================================================================

AT SELECTION-SCREEN ON VALUE-REQUEST FOR MFILENAM .

PERFORM F4_FOR_FILENAME .

  • ======================================================================

  • Initialization .

  • ======================================================================

Initialization .

T_tabl = 'Table Name' .

T_file = 'File Name' .

T_down = 'Download Table' .

T_chkf = 'Check File to Upload' .

T_upld = 'Upload File' .

T_show = 'Show Table Contents' .

  • ======================================================================

  • START OF SELECTION

  • ======================================================================

START-OF-SELECTION .

PERFORM CHECK_TABLE_NAME_IS_VALID .

  • ======================================================================

  • END OF SELECTION

  • ======================================================================

END-OF-SELECTION .

IF TABLE_NAME_IS_VALID EQ ' ' .

message i398(00) with 'INVALID TABLE NAME' .

ELSE .

PERFORM INSTANTIATE_DYNAMIC_INTERNAL_T .

CHECK DYNAMIC_IT_INSTANTIATED = 'X' .

CASE BUTTONSELECTED .

WHEN P_DOWNLD .

PERFORM SELECT_AND_DOWNLOAD .

when P_chkfil .

perform CHECK_FILE_TO_UPLOAD .

WHEN P_UPLOAD .

PERFORM UPLOAD_FROM_FILE .

when P_SHOW_T .

PERFORM SHOW_CONTENTS .

ENDCASE .

ENDIF .

&----


*& Form CHECK_TABLE_NAME_IS_VALID

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM CHECK_TABLE_NAME_IS_VALID.

DATA MCOUNT TYPE I .

tables dd02l .

CLEAR TABLE_NAME_IS_VALID .

SELECT COUNT(*) INTO MCOUNT FROM TADIR

WHERE PGMID = 'R3TR'

AND OBJECT = 'TABL'

AND OBJ_NAME = MTABLE_N .

IF MCOUNT EQ 1 .

clear dd02l .

select single * from dd02l where TABNAME = MTABLE_N .

IF SY-SUBRC eq 0.

if dd02l-tabclass = 'TRANSP' .

TABLE_NAME_IS_VALID = 'X' .

endif .

ENDIF.

ENDIF .

ENDFORM. " CHECK_TABLE_NAME_IS_VALID

&----


*& Form SELECT_AND_DOWNLOAD

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM SELECT_AND_DOWNLOAD.

CLEAR : <FS_ITAB> .

SELECT * FROM (MTABLE_N)

INTO CORRESPONDING FIELDS OF TABLE <FS_ITAB> .

PERFORM CHECK_FILENAME.

CALL FUNCTION 'WS_DOWNLOAD'

EXPORTING

FILENAME = MFILENAM

FILETYPE = 'DAT'

TABLES

DATA_TAB = <FS_ITAB>

EXCEPTIONS

FILE_OPEN_ERROR = 1

FILE_WRITE_ERROR = 2

INVALID_FILESIZE = 3

INVALID_TYPE = 4

NO_BATCH = 5

UNKNOWN_ERROR = 6

INVALID_TABLE_WIDTH = 7

GUI_REFUSE_FILETRANSFER = 8

CUSTOMER_ERROR = 9

OTHERS = 10.

IF SY-SUBRC eq 0.

MESSAGE I398(00) WITH 'Table' mtable_n

'successfully downloaded to '

mfilenam .

ENDIF.

ENDFORM. " SELECT_AND_DOWNLOAD

&----


*& Form UPLOAD_FROM_FILE

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM UPLOAD_FROM_FILE.

DATA : ANS TYPE C .

DATA : lines_of_itab TYPE I .

DATA : MSY_SUBRC TYPE I .

CALL FUNCTION 'POPUP_TO_CONFIRM_STEP'

EXPORTING

TEXTLINE1 = 'Are you sure you wish to upload'

TEXTLINE2 = 'data from ASCII File to DB table '

TITEL = 'Confirmation of Data Upload'

IMPORTING

ANSWER = ANS.

if ans = 'J' .

PERFORM CHECK_FILENAME.

CLEAR MSY_SUBRC .

CALL FUNCTION 'WS_UPLOAD'

EXPORTING

FILENAME = MFILENAM

FILETYPE = 'DAT'

TABLES

DATA_TAB = <FS_ITAB>

EXCEPTIONS

CONVERSION_ERROR = 1

FILE_OPEN_ERROR = 2

FILE_READ_ERROR = 3

INVALID_TYPE = 4

NO_BATCH = 5

UNKNOWN_ERROR = 6

INVALID_TABLE_WIDTH = 7

GUI_REFUSE_FILETRANSFER = 8

CUSTOMER_ERROR = 9

OTHERS = 10.

MSY_SUBRC = MSY_SUBRC + SY-SUBRC .

IF SY-SUBRC eq 0.

describe table <FS_ITAB> lines lines_of_itab .

IF LINES_OF_ITAB GT 0 .

DELETE (MTABLE_N) FROM TABLE <fs_itab> .

commit work .

insert (MTABLE_N) FROM TABLE <fs_itab> .

MSY_SUBRC = MSY_SUBRC + SY-SUBRC .

ENDIF .

ENDIF.

IF MSY_SUBRC EQ 0 .

MESSAGE I398(00) WITH LINES_OF_ITAB

'Record(s) inserted in table'

MTABLE_N .

ELSE .

MESSAGE I398(00) WITH

'Errors occurred No Records inserted in table'

MTABLE_N .

ENDIF .

endif .

ENDFORM. " UPLOAD_FROM_FILE

&----


*& Form F4_FOR_FILENAME

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM F4_FOR_FILENAME.

CALL FUNCTION 'WS_FILENAME_GET'

EXPORTING

DEF_PATH = 'C:\'

MASK = ',.,..'

MODE = '0'

IMPORTING

FILENAME = MFILENAM

EXCEPTIONS

INV_WINSYS = 1

NO_BATCH = 2

SELECTION_CANCEL = 3

SELECTION_ERROR = 4

OTHERS = 5.

ENDFORM. " F4_FOR_FILENAME

&----


*& Form CHECK_FILENAME

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM CHECK_FILENAME.

IF MFILENAM IS INITIAL

and not ( MTABLE_N is initial )

AND P_show_t ne BUTTONSELECTED.

CONCATENATE 'C:\' MTABLE_N '.TXT' INTO MFILENAM.

ENDIF .

ENDFORM. " CHECK_FILENAME

&----


*& Form INSTANTIATE_DYNAMIC_INTERNAL_T

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM INSTANTIATE_DYNAMIC_INTERNAL_T.

CLEAR DYNAMIC_IT_INSTANTIATED .

  • -----> Step 1 - Finding Field Names and ALV GRID Fieldcatalog

I_STRUCTURE_NAME = MTABLE_N .

CLEAR IT_FIELDCAT[] .

CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'

EXPORTING

I_STRUCTURE_NAME = I_STRUCTURE_NAME

CHANGING

CT_FIELDCAT = IT_FIELDCAT[]

EXCEPTIONS

INCONSISTENT_INTERFACE = 1

PROGRAM_ERROR = 2

OTHERS = 3.

IF SY-SUBRC EQ 0.

  • -----> Step 2 - Creating Field Catalog of the Object

  • cl_alv_table_create

LOOP AT IT_FIELDCAT .

CLEAR WA_FIELDCATALOG .

MOVE-CORRESPONDING IT_FIELDCAT TO WA_FIELDCATALOG .

WA_FIELDCATALOG-REF_FIELD = IT_FIELDCAT-fieldname .

WA_FIELDCATALOG-REF_TABLE = mtable_n .

APPEND WA_FIELDCATALOG TO it_fieldcatALOG .

ENDLOOP .

  • -----> Step 3 - Creating Internal Table Dynamicaly

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = it_fieldcatALOG

IMPORTING

ep_table = MITAB .

ASSIGN MITAB->* TO <FS_ITAB> .

DYNAMIC_IT_INSTANTIATED = 'X' .

ENDIF.

ENDFORM. " INSTANTIATE_DYNAMIC_INTERNAL_T

&----


*& Form SHOW_CONTENTS

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM SHOW_CONTENTS.

CLEAR : <FS_ITAB> .

SELECT * FROM (MTABLE_N)

INTO CORRESPONDING FIELDS OF TABLE <FS_ITAB> .

I_CALLBACK_PROGRAM = SY-REPID .

CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'

EXPORTING

I_CALLBACK_PROGRAM = I_CALLBACK_PROGRAM

IT_FIELDCAT = IT_FIELDCAT[]

TABLES

T_OUTTAB = <FS_ITAB>

EXCEPTIONS

PROGRAM_ERROR = 1

OTHERS = 2.

ENDFORM. " SHOW_CONTENTS

&----


*& Form CHECK_FILE_TO_UPLOAD

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM CHECK_FILE_TO_UPLOAD.

PERFORM CHECK_FILENAME.

  • CLEAR MSY_SUBRC .

CALL FUNCTION 'WS_UPLOAD'

EXPORTING

FILENAME = MFILENAM

FILETYPE = 'DAT'

TABLES

DATA_TAB = <FS_ITAB>

EXCEPTIONS

CONVERSION_ERROR = 1

FILE_OPEN_ERROR = 2

FILE_READ_ERROR = 3

INVALID_TYPE = 4

NO_BATCH = 5

UNKNOWN_ERROR = 6

INVALID_TABLE_WIDTH = 7

GUI_REFUSE_FILETRANSFER = 8

CUSTOMER_ERROR = 9

OTHERS = 10.

  • MSY_SUBRC = MSY_SUBRC + SY-SUBRC .

IF SY-SUBRC eq 0.

I_CALLBACK_PROGRAM = SY-REPID .

CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'

EXPORTING

I_CALLBACK_PROGRAM = I_CALLBACK_PROGRAM

IT_FIELDCAT = IT_FIELDCAT[]

TABLES

T_OUTTAB = <FS_ITAB>

EXCEPTIONS

PROGRAM_ERROR = 1

OTHERS = 2.

endif .

ENDFORM. " CHECK_FILE_TO_UPLOAD