Internal table with header line - abap

We have class which process items from document in modul FI. I am trying Select data into Internal Table and after that count vat. But its raising error in declaration
DATA: IT_BSEG TYPE TABLE IT_VBSEG WITH HEADER LINE.
LOOP AT IT_BSEG.
IT_VBSEG-FIELX = IT_VBSEG-FIELX + wa_x.
modify IT_VBSEG.
ENDLOOP.

In ABAP OO you cannot use internal tables with header line. Try to modify your program using a field symbol:
DATA: IT_BSEG TYPE TABLE IT_VBSEG.
FIELD-SYMBOLS: <fs_bseg> like line of IT_BSEG.
LOOP AT IT_BSEG ASSIGNING <fs_bseg>.
<fs_bseg>-FIELX = <fs_bseg>-FIELX + wa_x.
ENDLOOP.

Related

Assigning Field Symbols to Internal Table

I'm trying upload Excel file to internal table in ABAP. I'm using function GUI_UPLOAD and then SCMS_BINARY_TO_XSTRING. At last I have field sybmbol <gt_data> with data from Excel file.
DATA(lo_data_ref) = lo_excel_ref->if_fdt_doc_spreadsheet~get_itab_from_worksheet(
lv_woksheetname ).
*-- Excel work sheet data in dyanmic internal table
ASSIGN lo_data_ref->* TO <gt_data>.
A [CString]
B [CString]
data1
data11
data2
data22
data3
data33
How I can iterate <gt_data> to internal table? I would try like below, but I received dump.
TYPES: BEGIN OF lty_test,
A TYPE string,
B TYPE string,
END OF lty_test.
DATA: lt_test_table TYPE STANDARD TABLE OF lty_test.
As far as I understood, you want to read excel rows with this code.
LOOP AT <gt_data> ASSIGNING FIELD-SYMBOL(<ls_data>).
ENDLOOP.
I am not sure struct of <ls_data> but I think you can read it with index for get to know main idea.
Could you try it like below?
CHECK <gt_data> IS ASSIGNED.
"It's column count for excel file. It can be found dynamically.
DATA(lv_column_count) = 10.
"Loop for rows.
LOOP AT <gt_data> ASSIGNING FIELD-SYMBOL(<ls_data>).
"Loop for columns
DO lv_column_count.
ASSIGN COMPONENT sy-index OF <ls_dat> TO FIELD-SYMBOL(<lfs_value>).
ENDDO.
ENDLOOP.
So first you need to declare a structure for the internal table and each field in the structure should have type "string".
Types:begin of ty_upload,
Field1 type string,
Field2 type string,
End of ty_upload.
Data:it_upload type standard table of ty_upload.
It_upload[] = <gt_data>
Now internal table it_upload should have the data from the field value
Try this:
file = 'C:\xyz.XLS'.
CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
EXPORTING
filename = file
i_begin_col = '1'
i_begin_row = '1'
i_end_col = '5'
i_end_row = '6000'
TABLES
intern = xcel
EXCEPTIONS
inconsistent_parameters = 1
upload_ole = 2
OTHERS = 3.
LOOP AT xcel.
" xcel is an internal table and has field xcel-value
ENDLOOP.

Why do I get runtime error DBSQL_DBSL_LENGTH_ERROR?

I have the following code:
DATA: lt_matnr TYPE TABLE OF mara,
ls_matnr TYPE mara,
lv_werk TYPE werks_d VALUE 'WERK',
lt_stoc_int TYPE TABLE OF zmm_s_stock_list,
lt_stoc TYPE TABLE OF zsd_stock_list,
ls_stoc TYPE zsd_stock_list.
SELECT matnr
FROM mara
INTO CORRESPONDING FIELDS OF TABLE lt_matnr.
LOOP AT lt_matnr INTO ls_matnr.
CALL FUNCTION 'Z_MM_LIST_STOC_MATERIAL_WERKS'
EXPORTING
IP_MATNR = ls_matnr-matnr
IP_WERKS = lv_werk
IMPORTING
ET_STOCK_EXP = lt_stoc_int.
LOOP AT lt_stoc_int ASSIGNING FIELD-SYMBOL(<ls_stoc_int>).
MOVE-CORRESPONDING <ls_stoc_int> TO ls_stoc.
* + other data processing ...
APPEND ls_stoc TO lt_stoc.
ENDLOOP.
ENDLOOP.
INSERT zsd_stock_list FROM TABLE lt_stoc.
Everything works fine until the INSERT statement where I get the following short-dump:
Runtime error: DBSQL_DBSL_LENGTH_ERROR
Exception: CX_SY_OPEN_SQL_DB
Error analysis:
An exception has occurred which is explained in more detail below. The
exception, which is assigned to class 'CX_SY_OPEN_SQL_DB' was not caught an
therefore caused a runtime error. The reason for the exception is:
While accessing a database, the length of a field in ABAP does not
match the size of the corresponding database field.
This can happen for example if a string is bound to a database field
that is shorter than the current string.
It makes little sense because lt_stoc is TYPE TABLE OF zsd_stock_list, how can the field length not match ?

read from internal table with loop and no results

In the below code, I read data from database table with SELECT into the internal table dresult.
Reading the first line of the internal table displays the expected result, but using the LOOP displays nothing.
What's the issue?
Note: I tried to remove ENDSELECT and it worked. Why?
REPORT ZTEST02.
"Detail Informaion
TYPES : BEGIN OF details,
d1 type arktx, " description
d2 type lfimg, "quantity
END OF details.
DATA : dresult TYPE TABLE OF details WITH HEADER LINE,
t_dresult TYPE details,
sdno TYPE vbeln.
PARAMETERS packo TYPE vbeln OBLIGATORY MATCHCODE OBJECT f4_likp.
START-OF-SELECTION.
SELECT arktx,lfimg
INTO #dresult
FROM lips as detail
LEFT JOIN marm as material
ON detail~matnr = material~matnr
LEFT OUTER JOIN vbak
ON detail~vgbel = vbak~vbeln
WHERE detail~vbeln = #packo.
ENDSELECT.
READ TABLE dresult into t_dresult INDEX 1.
write: t_dresult-d1,t_dresult-d2.
LOOP AT dresult INTO t_dresult.
write: t_dresult-d1,t_dresult-d2.
ENDLOOP.
With SELECT ... ENDSELECT you select the data into a work area (INTO #dresult - which is actually the header line of the internal table with the same name). As result the internal table dresult does not contain any data, so the LOOP and the READ TABLE won't work.
I would declare the internal table without HEADER LINE and remove ENDSELECT:
DATA: dresult type STANDARD TABLE OF details,
...
SELECT ...
INTO TABLE #dresult
Moreover, after READ TABLE, it is always good to check if an entry was found:
READ TABLE ...
IF sy-subrc EQ 0.
...
ENDIF.

Convert work area to the line of type internal table

I am getting this syntax error:
"WA_1" cannot be converted to the line type of "IT_1"
I'm trying to display contents from internal table as follows:
REPORT ZSAM.
DATA: ITable Type ZMUAZ_STRUCTURE OCCURS 10,
IT_1 Type ZSTRUCT1 OCCURS 10,
IT_2 Type ZSTRUCT2 OCCURS 10,
WA_1 like IT_1,
WA_2 like IT_2,
WA_3 like ITable.
WRITE: 'vbeln', 'vtweg', 'posnr', 'matnrr','vrkme'.
select vbeln audat netwr waerk vkorg vtweg from VBAK into corresponding fields of Table IT_1.
LOOP AT IT_1 INTO WA_1.
write: / WA_1-vbeln, WA_1-audat, WA_1-netwr, WA_1-waerk, WA_1-vkorg, WA_1-vtweg.
endloop.
Any idea how to resolve this error?
wa_1 is declared as internal table (not as work area). The simplest solution is to complete the declaration with LINE OF:
... wa_1 LIKE LINE OF it_1,
However declaring internal tables and work areas like you did with OCCURS is obsolete, its modern equivalent is STANDARD TABLE OF :
DATA: it_1 TYPE STANDARD TABLE OF zstruct1,
wa_1 TYPE zstruct1.

What is the difference between like and like line of in ABAP?

I have one doubt. May I know what the difference between LIKE and LIKE LINE OF in ABAP is? I have seen somewhere that while declaring the work area they are declaring.
wa LIKE it_one
wa LIKE LINE OF it_one
LIKE LINE OF means that the variable will be of the table line type.
LIKE means that the variable will be exactly of the same type as the one sitting after this key word.
Example
TYPES: BEGIN OF t_my_example_structure,
my_example_field1 TYPE i,
my_example_field2 TYPE n,
END OF t_my_example_structure.
TYPES tt_my_example_structure TYPE STANDARD TABLE OF t_my_example_structure.
DATA: l_tab_my_example TYPE tt_my_example_structure.
* has structure of row of l_tab_my_example so in this case t_my_example_structure.
DATA: l_str_my_example LIKE LINE OF l_tab_my_example.
* is exactly the same table type as l_tab_my_example so in this case tt_my_example_structure.
DATA: l_tab_like_my_example LIKE l_tab_my_example.
* I use it often for LOOP AT <tab> ASSIGNING <fs>.
FIELD-SYMBOLS: <fs_str_my_example> LIKE LINE OF l_tab_my_example.
Well, the difference is when you pass table into subroutine with USING or TABLES.
In 1st case you will get a table without headerline, thus WA_LIKE will be a table too.
In 2nd case IT_DATA will be a table with headerline: this causes IT_DATA actually means IT_DATA as structure or IT_DATA[] as table, depending on context. Particulary, DATA ... LIKE IT_DATA will refer to headerline, and not entire internal table.
You may check this using a debugger:
DATA T_DATA TYPE STRING_TABLE.
PERFORM TEST_01 USING T_DATA.
PERFORM TEST_02 TABLES T_DATA.
FORM TEST_01 USING IT_DATA TYPE STRING_TABLE.
DATA : WA_LIKE LIKE IT_DATA "This is a Table
, WA_LINE LIKE LINE OF IT_DATA.
BREAK-POINT.
ENDFORM.
FORM TEST_02 TABLES IT_DATA TYPE STRING_TABLE.
DATA : WA_LIKE LIKE IT_DATA "This is a String
, WA_LINE LIKE LINE OF IT_DATA.
BREAK-POINT.
ENDFORM.