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: 

Insertion of Records directly from Internal Table to Database Table

Former Member
0 Kudos

HI all,

I want to insert the data to database table directly from internal table .I have written following program.

DATA : IT_TAB1 type table of FAGLFLEXA with header line.

start-OF-SELECTION .

SELECT * FROM FAGLFLEXA into CORRESPONDING FIELDS OF TABLE IT_TAB1

WHERE RYEAR = '2008'

AND DOCNR = '0041017314'

AND RBUKRS = 'NCL'

" AND BSCHL = '50'

AND RACCT = '0000245612' .

LOOP at IT_TAB1 ."where BSCHL = '40'.

IT_TAB1-DOCNR = '0041017314' .

IT_TAB1-DOCLN = '000008' .

IT_TAB1-RACCT = '0000245612' .

IT_TAB1-PRCTR = '0000002001' .

IT_TAB1-PPRCTR = '0000002002' .

IT_TAB1-TSL = '10241.00'.

IT_TAB1-WSL = '10241.00' .

IT_TAB1-HSL = '10241.00' .

IT_TAB1-BUDAT = '20081018' .

IT_TAB1-BSCHL = '50' .

IT_TAB1-DRCRK = 'H' .

IT_TAB1-BELNR = '0041017314' .

MODIFY IT_TAB1 TRANSPORTING DOCNR DOCLN RACCT PRCTR PPRCTR TSL WSL HSL BSCHL DRCRK BELNR .

ENDLOOP .

if IT_TAB1[] is NOT INITIAL .

MODIFY FAGLFLEXA from TABLE IT_TAB1[].

COMMIT WORK.

endif.

It works fine. I want to insert 40 records in database table .How can i insert this records by uploading text or excel file. Please explan it with code.

Regard's

Atul

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Use GUI_UPLOAD FM . Get all the data from the file into your internal table and process it as to how you normally do.

6 REPLIES 6

Former Member
0 Kudos

Use GUI_UPLOAD FM . Get all the data from the file into your internal table and process it as to how you normally do.

Former Member
0 Kudos

hi

you need to do two things

1. use f4_filename function module to get file name from presentation server

2. use gui_upload to upload file data into itab

rest will be as you are doing in your program

Former Member
0 Kudos

hi

Check This

wa-->Work area

Database table name ---> zdrsample

Data: wa type stru.--->stru --Structure of the table

wa-ss : = 800.

wa-ssno : = TENO.

wa-ssname = TENAME .

wa-ssadd : = TEADD.

insert into zdrsample values wa.--->Internable to Database table

Thanks

Dharma

Former Member
0 Kudos

hi

try this

1) To upload data from Excel sheet (.xls) to Sap Internal table

TYPE-POOLS truxs.

DATA : BEGIN OF itab OCCURS 0,

sname(15) TYPE c,

scity(20) TYPE c,

sage(3) TYPE c,

END OF itab.

DATA : it_type TYPE truxs_t_text_data.

PARAMETER p_file TYPE rlgrap-filename.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

CALL FUNCTION 'F4_FILENAME'

EXPORTING

field_name = 'P_FILE'

IMPORTING

file_name = p_file.

START-OF-SELECTION.

CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'

EXPORTING

i_tab_raw_data = it_type

i_filename = p_file

TABLES

i_tab_converted_data = ITAB[]

EXCEPTIONS

conversion_failed = 1

OTHERS = 2.

2) To upload data from text file (.txt) to Sap Internal table

DATA : BEGIN OF itab OCCURS 0,

sname(15) TYPE c,

scity(20) TYPE c,

sage(3) TYPE c,

END OF itab.

DATA : w_file TYPE string.

PARAMETER p_file TYPE rlgrap-filename.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

CALL FUNCTION 'F4_FILENAME'

EXPORTING

field_name = 'P_FILE'

IMPORTING

file_name = p_file.

START-OF-SELECTION.

w_file = p_file.

CALL FUNCTION 'GUI_UPLOAD'

EXPORTING

filename = w_file

filetype = 'ASC'

has_field_separator = 'X'

TABLES

data_tab = itab.

IF sy-subrc <> 0.

ENDIF.

Regards

Sm1tje
Active Contributor
0 Kudos

I really hope that you know what you are doing here!!!! This is a standard SAP table, in which you are inserting data directly without any check or what so ever. My advise would be, to NEVER EVER update SAP tables directly with INSERT, UPDATE or MODIFY.

There is a thing called referential integrity, meaning in a nutshell, there are other tables related to this table, which will not be updated when you are inserting data. Also, there are several customizing tables 'assigned' to this table. In other words all the data that you are entering manually into this table, will be checked against customizing / check tables etc. But upon direct insert, this will not be the case.

ALWAYS use the proper methods like BDC, LSMW, BAPI's/FM, to update standard tables.

Former Member
0 Kudos

Hi Atul ,

You can do it from the internal table .

Inside the loop if the SY-TABIX is greater than 40, then exit .

I changed your code a bit , please check.

DATA : IT_TAB1 type table of FAGLFLEXA with header line.

start-OF-SELECTION .

SELECT * FROM FAGLFLEXA into CORRESPONDING FIELDS OF TABLE IT_TAB1

WHERE RYEAR = '2008'
AND DOCNR = '0041017314'
AND RBUKRS = 'NCL'
" AND BSCHL = '50'
AND RACCT = '0000245612' .

LOOP at IT_TAB1 ."where BSCHL = '40'.

IT_TAB1-DOCNR = '0041017314' .
IT_TAB1-DOCLN = '000008' .
IT_TAB1-RACCT = '0000245612' .
IT_TAB1-PRCTR = '0000002001' .
IT_TAB1-PPRCTR = '0000002002' .
IT_TAB1-TSL = '10241.00'.
IT_TAB1-WSL = '10241.00' .
IT_TAB1-HSL = '10241.00' .
IT_TAB1-BUDAT = '20081018' .
IT_TAB1-BSCHL = '50' .
IT_TAB1-DRCRK = 'H' .
IT_TAB1-BELNR = '0041017314' .
IF SY_TABIX  = 40.  " When 40 records are inserted
EXIT.    "  Exit
ENDIF.   " Endif
MODIFY IT_TAB1 TRANSPORTING DOCNR DOCLN RACCT PRCTR PPRCTR TSL WSL HSL BSCHL DRCRK BELNR .
ENDLOOP .

if IT_TAB1[] is NOT INITIAL .
MODIFY FAGLFLEXA from TABLE IT_TAB1[].
COMMIT WORK.
endif.

Regards

Pinaki