‎2008 May 23 8:06 AM
Hi,
I have data in an internal table which I got from an excel file using FM.Now I need to create a database table and populate the value of internal table into that.How do I proceed now?
My code till now is:
&----
*& Report ZUPLOADFILETOINTERN
*&
&----
*&
*&
&----
REPORT ZUPLOADFILETOINTERN.
*Data Declaration
*----
tables: rlgrap.
data: btab like zalsmex_tabline occurs 0 with header line.
data: dtab like zalsmex_tabline occurs 0 with header line.
TYPES: Begin of t_record,
fld1 type c length 50,
fld2 type c length 50,
fld3 type c length 50,
fld4 type c length 50,
fld5 type c length 50,
fld6 type c length 50,
fld7 type c length 50,
fld8 type c length 50,
fld9 type c length 50,
fld10 type c length 50,
fld11 type c length 50,
fld12 type c length 50,
fld13 type c length 50,
fld14 type c length 50,
fld15 type c length 50,
End of t_record.
DATA var1 type i.
DATA var2 type i.
DATA str1 type string.
DATA: it_record type standard table of t_record initial size 0 with key fld1,
wa_record type t_record.
DATA: dt_record type standard table of t_record initial size 0 with key fld1,
da_record type t_record.
DATA: gd_currentrow type i.
constants n type p value 2.
*Selection Screen Declaration
*----
parameters p_infile like rlgrap-filename.
************************************************************************
*START OF SELECTION of file
data: l_filename like RLGRAP-FILENAME.
at selection-screen on value-request for p_infile.
CALL FUNCTION 'KD_GET_FILENAME_ON_F4'
EXPORTING
PROGRAM_NAME = SYST-REPID
DYNPRO_NUMBER = SYST-DYNNR
FIELD_NAME = ' '
STATIC = ' '
MASK = ' '
CHANGING
FILE_NAME = p_infile
EXCEPTIONS
MASK_TOO_LONG = 1
OTHERS = 2
.
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
*select the header data and copy into IT_RECORD.
start-of-selection.
call function 'ZALSM_EXCEL_TO_INTERNAL_TABL'
exporting
filename = p_infile
i_begin_col = '1'
i_begin_row = '1'
i_end_col = '1'
i_end_row = '17'
tables
zintern = btab
exceptions
inconsistent_parameters = 1
upload_ole = 2
others = 3.
if sy-subrc <> 0.
message e030(zz) with text-001. "Problem uploading Excel Spreadsheet"
endif.
Sort table by rows and colums
sort btab by row col.
Get first row retrieved
read table btab index 1.
Set first row retrieved to current row
gd_currentrow = btab-row.
loop at btab.
Reset values for next row
if btab-row ne gd_currentrow.
append wa_record to it_record.
clear wa_record.
gd_currentrow = btab-row.
endif.
case btab-col.
when '0001'.
wa_record-fld1 = btab-value.
when '0002'.
wa_record-fld2 = btab-value.
when '0003'.
wa_record-fld3 = btab-value.
when '0004'.
wa_record-fld4 = btab-value.
when '0005'.
wa_record-fld5 = btab-value.
when '0006'.
wa_record-fld6 = btab-value.
when '0007'.
wa_record-fld7 = btab-value.
when '0008'.
wa_record-fld8 = btab-value.
when '0009'.
wa_record-fld9 = btab-value.
when '0030'.
wa_record-fld10 = btab-value.
when '0011'.
wa_record-fld11 = btab-value.
when '0012'.
wa_record-fld12 = btab-value.
when '0013'.
wa_record-fld13 = btab-value.
when '0014'.
wa_record-fld14 = btab-value.
endcase.
endloop.
append wa_record to it_record.
*!! Excel data is now contained within the internal table IT_RECORD
Display report data for illustration purposes
Read FROM and TO period values into variables.
read table it_record index 15 into var1 .
read table it_record index 16 into var2.
*Read category from the header into string str1.
read table it_record index 14 into str1.
var2 = var2 + 2 .
var1 = var1 + 2.
IF var1 > var2.
message e030(zz) with text-001. "From Value cannot be greater than TO value"
ENDIF.
*Start of selection of item data.
call function 'ZALSM_EXCEL_TO_INTERNAL_TABL'
exporting
filename = p_infile
i_begin_col = 1
i_begin_row = '18'
i_end_col = 14
i_end_row = '10000'
tables
zintern = dtab
exceptions
inconsistent_parameters = 1
upload_ole = 2
others = 3.
if sy-subrc <> 0.
message e030(zz) with text-001. "Problem uploading Excel Spreadsheet"
endif.
Sort table by rows and colums
sort dtab by row col.
Get first row retrieved
read table dtab index 1.
Set first row retrieved to current row
gd_currentrow = dtab-row.
loop at dtab.
Reset values for next row
if dtab-row ne gd_currentrow.
append da_record to dt_record.
clear da_record.
gd_currentrow = dtab-row.
endif.
case 1.
when '0001'.
endcase.
da_record-fld3 = str1.
case dtab-col.
when '0001' .
if 0001 eq 1.
da_record-fld1 = dtab-value.
endif.
when '0002'.
if 0002 eq 2.
da_record-fld2 = dtab-value.
endif.
when '0003'.
if 0003 between var1 and var2.
da_record-fld4 = dtab-value.
endif.
when '0004'.
if 0004 between var1 and var2.
da_record-fld5 = dtab-value.
endif.
when '0005'.
if 0005 between var1 and var2.
da_record-fld6 = dtab-value.
endif.
when '0006'.
if 0006 between var1 and var2.
da_record-fld7 = dtab-value.
endif.
when '0007'.
if 0007 between var1 and var2.
da_record-fld8 = dtab-value.
endif.
when '0008'.
if 0008 between var1 and var2.
da_record-fld9 = dtab-value.
endif.
when '0009'.
if 0009 between var1 and var2.
da_record-fld10 = dtab-value.
endif.
when '0010'.
if 0010 between var1 and var2.
da_record-fld11 = dtab-value.
endif.
when '0011'.
if 0011 between var1 and var2.
da_record-fld12 = dtab-value.
endif.
when '0012'.
if 0012 between var1 and var2.
da_record-fld13 = dtab-value.
endif.
when '0013'.
if 0013 between var1 and var2.
da_record-fld14 = dtab-value.
endif.
when '0014'.
if 0014 between var1 and var2.
da_record-fld15 = dtab-value.
endif.
endcase.
endloop.
append da_record to dt_record.
*!! Excel data is now contained within the internal table DT_RECORD
Write da_record for illustration purposes.
loop at dt_record into da_record.
write:/ sy-vline,
(15) da_record-fld1,
(15) da_record-fld2,
(15) da_record-fld3,
(10) da_record-fld4,
(10) da_record-fld5,
(10) da_record-fld6,
(10) da_record-fld7,
(10) da_record-fld8,
(10) da_record-fld9,
(10) da_record-fld10,
(10) da_record-fld11,
(10) da_record-fld12,
(10) da_record-fld13,
(10) da_record-fld14,
(10) da_record-fld15.
endloop.
Ankit
‎2008 May 23 8:11 AM
Hi Friend,
Create a internal table same structure like the database table.
Populate the data into that internal table.
Use Modify command to modify the database table.
(Modify <databasetable> from <internal table>)
Regards
Krishnendu
‎2008 May 23 8:17 AM
Hi,
use the below mentioned comd to move all the lines of the internal table to the database table.
INSERT <dbtab> FROM TABLE <itab>.
Reward if helpful.
Thanks.