2005 Nov 18 9:38 AM
Hi experts,
How to find dynamically column count for an internal table.
Please help me
2005 Nov 18 10:04 AM
Hi Murali,
You can have a look at prg BCALV_TABLE_CREATE.
Or can try out this following code:
REPORT ZTEST .
DATA: d_ref TYPE REF TO data,
d_ref2 TYPE REF TO data ,
i_alv_cat TYPE TABLE OF lvc_s_fcat,
ls_alv_cat LIKE LINE OF i_alv_cat.
TYPES tabname LIKE dcobjdef-name .
parameter: p_tablen type tabname.
data: begin of itab occurs 0.
INCLUDE STRUCTURE dntab.
data: end of itab.
FIELD-SYMBOLS : <F_FS> TYPE table,
<F_FS1> TYPE TABLE,
<F_FS2> TYPE ANY,
<F_FS3> TYPE TABLE.
REFRESH itab.
CALL FUNCTION 'NAMETAB_GET'
EXPORTING
langu = sy-langu
tabname = p_tablen
TABLES
nametab = itab
EXCEPTIONS
no_texts_found = 1.
LOOP AT itab .
ls_alv_cat-fieldname = itab-fieldname.
ls_alv_cat-ref_table = p_tablen.
ls_alv_cat-ref_field = itab-fieldname.
APPEND ls_alv_cat TO i_alv_cat.
ENDLOOP.
internal table build
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING it_fieldcatalog = i_alv_cat
IMPORTING ep_table = d_ref .
ASSIGN d_ref->* TO <F_FS>.
SELECT * FROM (p_tablen) INTO CORRESPONDING FIELDS OF TABLE <F_FS>.
LOOP AT <F_FS> ASSIGNING <F_FS2>.
*your code goes here.
ENDLOOP.
Regards,
Raj
2005 Nov 18 10:16 AM
Hi Murali,
If you just need the column count then do the below.
After declaring the itab. Call function module
'REUSE_ALV_FIELDCATALOG_MERGE' and apss itab structure
name. It will create field catalog table with all the
columns.
Find the itab count using
Describe table (tabname) lines count.
Count will give you the column count.
Hope this will work for you.
Thanks & Regards,
Siri.
Message was edited by: Srilatha T
2005 Nov 18 10:20 AM
Hi,
Here is one way
DATA spfli_wa TYPE spfli.
DATA count TYPE sy-tabix.
FIELD-SYMBOLS: <wa> TYPE ANY,
<comp> TYPE ANY.
ASSIGN spfli_wa TO <wa>.
SELECT SINGLE *
FROM spfli
INTO spfli_wa
WHERE carrid = 'LH' AND
connid = '400'.
WHILE sy-subrc = 0.
ASSIGN COMPONENT sy-index OF STRUCTURE <wa> TO <comp>.
WRITE / <comp>.
count = count + 1.
ENDWHILE.
WRITE / 'Total Columns:" , count.
2005 Nov 18 10:27 AM
hi,
one of them told you to use field_catlog merge but this works only if your internal table contains all dbfields declared using 'like'.
regards,
manohar.
2005 Nov 18 10:33 AM
Hi Murali,
How to find dynamically column count for an internal table.
1. THERE ARE TWO WAYS
-
1. Using the FM Field_Catalogue_Merge
(As mentioned in last reply)
2. Using Classes
-
2. METHOD 1 IS EASY
BUT THE DISADVANTAGE IS
IT PROVIDES INFO ONLY WHEN INTERNAL TABLE FIELDS
ARE DEFINED USING
LIKE
and not TYPE
3. METHOD 2 USING CLASSES IS ALSO ANOTHER WAY.
IT DOES NOT HAVE PROBLEM LIKE METHOD 1.
WITH THIS ONE CAN GET NOT ONLY THE COLUMN COUNT,
BUT THE COLUMN NAMES, TYPE, LENGTH .
4. BELOW IS THE SAMPLE CODE WHICH I TRIED JUST NOW
AT MY END.
*----
REPORT typedescr_test.
*------- Variables
data : det type ref to CL_ABAP_structDESCR.
data : wa like line of det->components.
*------ Internal Table
DATA : BEGIN OF ITAB OCCURS 0,
MANDT TYPE T001-MANDT, "--- type
PERNR LIKE P0001-PERNR, " --- like
MATNR TYPE MARA-MATNR, "--- type
EBELN LIKE EKKO-EBELN, "--- like
END OF ITAB.
*----
Start of selection
START-OF-SELECTION.
det ?= cl_abap_typedescr=>describe_by_DATA( ITAB ).
loop at det->components into wa.
write 😕 wa-name , wa-type_kind , wa-length.
endloop.
*----
Number Of columns
describe table det->components.
write /: sy-tfill.
*----
HOPE THE ABOVE HELPS.
Regards,
Amit M.
2005 Nov 18 10:33 AM
Hi please see the following code. it may help u.
FUNCTION ZMANDY_EXCEL_UPLOAD.
*"----
""Local interface:
*" IMPORTING
*" REFERENCE(FILE_NAME) TYPE RLGRAP-FILENAME DEFAULT
*" 'C:WINDOWSDESKTOP'
*" TABLES
*" INTERNAL_TAB
*"----
INCLUDE OLE2INCL.
DATA: STR(20),NDATE(10).
DATA: ROW TYPE I VALUE 1, COL TYPE I VALUE 1.
DATA OBJ TYPE OLE2_OBJECT.
DATA WORKBOOK TYPE OLE2_OBJECT.
DATA SH TYPE OLE2_OBJECT.
DATA CELL TYPE OLE2_OBJECT.
DATA RANGE TYPE OLE2_OBJECT.
DATA APP TYPE OLE2_OBJECT.
DATA SEL TYPE OLE2_OBJECT.
DATA COLUMNS TYPE OLE2_OBJECT.
DATA TYP.
FIELD-SYMBOLS: <F>.
CREATE OBJECT OBJ 'excel.application'.
SET PROPERTY OF OBJ 'visible' = 1.
CALL METHOD OF OBJ 'Workbooks' = WORKBOOK.
WRITE 😕 'workbook', SY-SUBRC.
CALL METHOD OF WORKBOOK 'add'.
CALL METHOD OF OBJ 'Worksheets' = SH EXPORTING #1 = 1.
CALL METHOD OF SH 'Activate'.
LOOP AT INTERNAL_TAB.
COL = 1.
do.
ASSIGN COMPONENT SY-INDEX OF STRUCTURE INTERNAL_TAB TO <F>.
IF SY-SUBRC <> 0.
EXIT.
ENDIF.
DESCRIBE FIELD <F>. "TYPE TYP COMPONENTS N.
CALL METHOD OF SH 'Cells' = CELL EXPORTING #1 = ROW #2 = COL.
SET PROPERTY OF CELL 'value' = <F>. "VALUE.
*PERFORM INSTERDATA USING ROW COL <F>.
COL = COL + 1.
ENDDO.
ROW = ROW + 1.
ENDLOOP.
STR = ROW.
ENDFUNCTION.
Satish
2005 Nov 18 10:48 AM
Hi,
If you just have to find the column count I think no need for field catalog merge and so on, just a simple DESCRIBE statment should do.
****Example with two types of internal table declarations
DATA: t_kna1 TYPE TABLE OF kna1 INITIAL SIZE 0,
BEGIN OF t_user OCCURS 0,
first TYPE i,
second,
third LIKE sy-datum,
END OF t_user,
ref_data TYPE REF TO DATA,
v_type,
v_count TYPE i.
FIELD-SYMBOLS: <wa_data> TYPE ANY,
****For table without header line
<wa_kna1> LIKE LINE OF t_kna1.
****Static - Without header line
DESCRIBE FIELD <wa_kna1> TYPE v_type COMPONENTS v_count.
WRITE: / v_type, v_count.
****Static - With header line
DESCRIBE FIELD t_user TYPE v_type COMPONENTS v_count.
WRITE: / v_type, v_count.
****Dynamic - Without header line
CREATE DATA ref_data LIKE LINE OF t_kna1.
ASSIGN ref_data->* TO <wa_data>.
DESCRIBE FIELD <wa_data> TYPE v_type COMPONENTS v_count.
WRITE: / v_type, v_count.
****Dynamic - With header line
CREATE DATA ref_data LIKE t_user.
ASSIGN ref_data->* TO <wa_data>.
DESCRIBE FIELD <wa_data> TYPE v_type COMPONENTS v_count.
WRITE: / v_type, v_count.
Hope this helps..
Sri
Message was edited by: Srikanth Pinnamaneni
2005 Nov 18 10:57 AM
2005 Nov 21 9:16 AM
Dear murali,
if ur query is solved,
u may pls award points
to the best answer or whomever u may like.
regards,
amit m.