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

Generic Insert in DBTAB

Former Member
0 Likes
1,558

Hi Gurus,

i have a difficult question, and i currently do not know how find a solution. I would like to add a entry in a database table, but i only know the name through runtime. So my insert should generic. I am able to create the Structure type of the table, but how do i assign the values to the components,

here is the source code i tried.

FIELD-SYMBOLS: <ITAB> TYPE TABLE.

FIELD-SYMBOLS: <FIELD> TYPE ANY.

FIELD-SYMBOLS: <ROW> TYPE ANY.

data: table_fields like dbfield occurs 0 with header line.

data: TABNAME LIKE DD02L-TABNAME Value 'ZKW1'.

data: itab type ref to data.

data: lrow type ref to data.

create data: itab type standard table of (tabname).

create data: lrow type (tabname).

assign: itab->* to <itab>.

assign: lrow to <row>.

call function 'DB_GET_TABLE_FIELDS'

exporting

  • FIELD_INFO = 'A'

tabname = tabname

  • IMPORTING

  • SUBRC =

tables

dbfields = table_fields

exceptions

others = 1.

sort table_fields by keyflag descending keypos column.

do.

read table table_fields index sy-index.

  • this will always fail, because the Structure is initial.

ASSIGN COMPONENT SY-INDEX OF STRUCTURE <row> TO

<FIELD>.

if sy-subrc <> 0.

exit.

endif.

<FIELD> = 'TEST'.

enddo.

insert (tabname) from <row>.

this is just a test, later i have the values of the table fields in an internal table with position of the field in the structure (Position = 1, Value = '123423'.

best regards,

Kai

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,423

Hi kai,

Probably this code may help a little.

1. To display data of internal table, using field-symbols

2. just copy paste

Report abc.

*----


data : itab like t001 occurs 0 with header line.

*----


field-symbols : <ft> type table.

FIELD-SYMBOLS: <dynline> TYPE ANY.

FIELD-SYMBOLS: <fld> TYPE ANY.

select * from t001 into table itab.

*----


assign itab[] to <FT>.

LOOP AT <ft> ASSIGNING <DYNLINE>.

sy-subrc = 0.

write 😕 '----


'.

WHILE SY-SUBRC = 0.

ASSIGN COMPONENT SY-INDEX OF STRUCTURE <DYNLINE> TO <FLD>.

WRITE : <FLD>.

ENDWHILE.

ENDLOOP.

regards,

amit m.

7 REPLIES 7
Read only

Former Member
0 Likes
1,423

Hi,

Why dont u try native SQL

Read only

0 Likes
1,423

you mean by defining the Fields and Values separately?

Can you explain that in more detail?

Read only

Former Member
0 Likes
1,423

hi Kai,

try this

REPORT ABC.

TABLES : DD03L.

PARAMETERS : DTAB LIKE DD03L-TABNAME.



SELECT SINGLE TABNAME FROM (DTAB) INTO DD03L-TABNAME WHERE TABNAME =
'MARA'.

where dtab is the parameter

Read only

Former Member
0 Likes
1,424

Hi kai,

Probably this code may help a little.

1. To display data of internal table, using field-symbols

2. just copy paste

Report abc.

*----


data : itab like t001 occurs 0 with header line.

*----


field-symbols : <ft> type table.

FIELD-SYMBOLS: <dynline> TYPE ANY.

FIELD-SYMBOLS: <fld> TYPE ANY.

select * from t001 into table itab.

*----


assign itab[] to <FT>.

LOOP AT <ft> ASSIGNING <DYNLINE>.

sy-subrc = 0.

write 😕 '----


'.

WHILE SY-SUBRC = 0.

ASSIGN COMPONENT SY-INDEX OF STRUCTURE <DYNLINE> TO <FLD>.

WRITE : <FLD>.

ENDWHILE.

ENDLOOP.

regards,

amit m.

Read only

0 Likes
1,423

Thanks for your help,

but my problem is not the display of data this is working correctly, my problem is how to fill the values of the Workarea <ROW> by specifying simply the position of the Field, because <row>-fieldname is not working because i do not know the name during designtime, only on runtime.

best regards,

Kai

Read only

0 Likes
1,423

Hi guys,

thaanks for your help,

but i fixed it on my own. For those who are interested here is a sample.

DATA LID LIKE DD02L-TABNAME VALUE '/CAMELOT/DOCS'.

DATA L_T_PARAMS TYPE /CAMELOT/DCPF_T_PARAMS.

DATA: L_S_PARAMS TYPE /CAMELOT/DCPF_S_PARAM.

DATA: TABLE_FIELDS LIKE DBFIELD OCCURS 0 WITH HEADER LINE.

DATA L_DATA TYPE REF TO DATA.

DATA L_S_FIELDS TYPE DBFIELD.

DATA L_COLUMN TYPE I.

FIELD-SYMBOLS: <FIELD> TYPE ANY,

<FIELD2> TYPE ANY..

L_S_PARAMS-NAME = 1.

L_S_PARAMS-VALUE = 12345.

APPEND L_S_PARAMS TO L_T_PARAMS.

L_S_PARAMS-NAME = 2.

L_S_PARAMS-VALUE = 'Meins'.

APPEND L_S_PARAMS TO L_T_PARAMS.

*Create empty Data

CREATE DATA L_DATA TYPE (LID).

  • Dereference Structure to fieldsymbol

ASSIGN L_DATA->* TO <FIELD>.

  • Get Field Definition

CALL FUNCTION 'DB_GET_TABLE_FIELDS'

EXPORTING

TABNAME = LID

TABLES

DBFIELDS = TABLE_FIELDS

EXCEPTIONS

OTHERS = 1.

SORT TABLE_FIELDS BY KEYFLAG DESCENDING KEYPOS COLUMN.

*

LOOP AT TABLE_FIELDS INTO L_S_FIELDS.

L_COLUMN = L_S_FIELDS-COLUMN.

CLEAR L_S_PARAMS.

ASSIGN COMPONENT L_COLUMN OF STRUCTURE <FIELD> TO <FIELD2>.

READ TABLE L_T_PARAMS INTO L_S_PARAMS WITH KEY NAME = L_COLUMN.

IF SY-SUBRC = 0.

MOVE L_S_PARAMS-VALUE TO <FIELD2>.

ENDIF.

ENDLOOP.

INSERT (LID) FROM <FIELD>.

Read only

kanchramas33
Associate
Associate
0 Likes
1,213

This is if you already know what the table is going to be. If you do not know the table /CAMELOT/DCPF_T_PARAMS, then you would use a generic type and then it would not let you use append.