Can you create an internal table dynamically? (at run time)
Yes , you can create a Dynamic Internal table .Just chek out this program .
type-pools : abap.
field-symbols: type standard table,
,
.
data: dy_table type ref to data,
dy_line type ref to data,
xfc type lvc_s_fcat,
ifc type lvc_t_fcat.
selection-screen begin of block b1 with frame.
parameters: p_table(30) type c default 'T001'.
selection-screen end of block b1.
start-of-selection.
perform get_structure.
perform create_dynamic_itab. **********Creates a dyanamic internal table**********
perform get_data.
perform write_out.
form get_structure.
data : idetails type abap_compdescr_tab,
xdetails type abap_compdescr.
data : ref_table_des type ref to cl_abap_structdescr.
* Get the structure of the table.
ref_table_des ?=
cl_abap_typedescr=>describe_by_name( p_table ).
idetails[] = ref_table_des->components[].
loop at idetails into xdetails.
clear xfc.
xfc-fieldname = xdetails-name .
xfc-datatype = xdetails-type_kind.
xfc-inttype = xdetails-type_kind.
xfc-intlen = xdetails-length.
xfc-decimals = xdetails-decimals.
append xfc to ifc.
endloop.
endform.
form create_dynamic_itab.
* Create dynamic internal table and assign to FS
call method cl_alv_table_create=>create_dynamic_table
exporting
it_fieldcatalog = ifc
importing
ep_table = dy_table.
assign dy_table->* to .
* Create dynamic work area and assign to FS
create data dy_line like line of .
assign dy_line->* to .
endform.
form get_data.
* Select Data from table.
select * into table
from (p_table).
endform.
Write out data from table.
loop at into .
do.
assign component sy-index
of structure to .
if sy-subrc <> 0.
exit.
endif.
if sy-index = 1.
write:/ .
else.
write: .
endif.
enddo.
endloop.
.......................................................................................................................................
REPORT ZCLUST1 .
*
* Example: how to create a dynamic internal table
*
* The dynamic internal table stucture
DATA: BEGIN OF STRUCT OCCURS 10,
FILDNAME(8) TYPE C,
ABPTYPE TYPE C,
LENGTH TYPE I,
END OF STRUCT.
* The dynamic program source table
DATA: BEGIN OF INCTABL OCCURS 10,
LINE(72),
END OF INCTABL.
DATA: LNG TYPE I, TYPESRTING(6).
* Sample dynamic internal table stucture
STRUCT-FILDNAME = 'field1'. STRUCT-ABPTYPE = 'c'. STRUCT-LENGTH = '6'.
APPEND STRUCT. CLEAR STRUCT.
STRUCT-FILDNAME = 'field2'. STRUCT-ABPTYPE = 'd'.
APPEND STRUCT. CLEAR STRUCT.
STRUCT-FILDNAME = 'field3'. STRUCT-ABPTYPE = 'i'.
APPEND STRUCT. CLEAR STRUCT.
* Create the dynamic internal table definition in the dyn. program
INCTABL-LINE = 'program zdynpro.'. APPEND INCTABL.
INCTABL-LINE = 'data: begin of dyntab occurs 10,'. APPEND INCTABL.
LOOP AT STRUCT.
INCTABL-LINE = STRUCT-FILDNAME.
LNG = STRLEN( STRUCT-FILDNAME ).
IF NOT STRUCT-LENGTH IS INITIAL .
TYPESRTING(1) = '('.
TYPESRTING+1 = STRUCT-LENGTH.
TYPESRTING+5 = ')'.
CONDENSE TYPESRTING NO-GAPS.
INCTABL-LINE+LNG = TYPESRTING.
ENDIF.
INCTABL-LINE+15 = 'type '.
INCTABL-LINE+21 = STRUCT-ABPTYPE.
INCTABL-LINE+22 = ','.
APPEND INCTABL.
ENDLOOP.
INCTABL-LINE = 'end of dyntab. '.
APPEND INCTABL.
* Create the code processes the dynamic internal table
INCTABL-LINE = ' '. APPEND INCTABL.
INCTABL-LINE = 'dyntab-field1 = ''aaaaaa''.'. APPEND INCTABL.
INCTABL-LINE = 'dyntab-field1 = ''19970814''.'. APPEND INCTABL.
INCTABL-LINE = 'dyntab-field1 = 1.'. APPEND INCTABL.
INCTABL-LINE = 'append dyntab.'. APPEND INCTABL.
INCTABL-LINE = ' '. APPEND INCTABL.
INCTABL-LINE = 'loop at dyntab.'. APPEND INCTABL.
INCTABL-LINE = 'write: / dyntab.'. APPEND INCTABL.
INCTABL-LINE = 'endloop.'. APPEND INCTABL.
* Create and run the dynamic program
INSERT REPORT 'zdynpro'(001) FROM INCTABL.
SUBMIT ZDYNPRO.
.......................................................................................................................................
or Just try out this simpler dynamic internal tables
DATA: itab TYPE STANDARD TABLE OF spfli,
wa LIKE LINE OF itab.
DATA: line(72) TYPE c,
list LIKE TABLE OF line(72).
START-OF-SELECTION.
*line = ' CITYFROM CITYTO '.
line = ' AIRPTO '.
APPEND line TO list.
SELECT DISTINCT (list)
INTO CORRESPONDING FIELDS OF TABLE itab
FROM spfli.
IF sy-subrc EQ 0.
LOOP AT itab INTO wa.
* WRITE: / wa-cityfrom, wa-cityto.
WRITE :/ wa-airpto.
ENDLOOP.
ENDIF.
Friday, April 18, 2008
How to create a Dynamic Internal Table or Array? SAP ABAP INTERVIEW Questions
Posted by Anonymous at 6:33 AM
Labels: ABAP Interview Questions
Subscribe to:
Post Comments (Atom)
Content
-
►
2009
(2)
- ► 09/06 - 09/13 (1)
- ► 03/01 - 03/08 (1)
-
▼
2008
(207)
- ► 11/23 - 11/30 (1)
- ► 04/20 - 04/27 (70)
-
▼
04/13 - 04/20
(51)
- Difference Between Select-Options & Ranges SAP ABA...
- Inner Join to retrieve the Material Valuation Clas...
- How to used 3 tables for inner joins? SAP ABAP INT...
- Protect Selection/Parameters SAP ABAP INTERVIEW Qu...
- Difference Between Select Single and Select UpTo O...
- Select statement with inner join is taking forever...
- How can we give dynamic table name in select state...
- Performance tuning for Data Selection Statement SA...
- What's the purpose of using PACKAGE SIZE in select...
- Usage of 'for all entries' in Select Statement SAP...
- ABAP Self Test Q & A SAP ABAP INTERVIEW Questions
- What is the difference between SMOD and CMOD? SAP ...
- Split String into two parts at delimiter SAP ABAP ...
- String Handling in ABAP - Removing Unwanted Char S...
- System Fields for Details Lists SAP ABAP INTERVIEW...
- System Landscape: SAP ABAP INTERVIEW Questions
- System Landscape1 SAP ABAP INTERVIEW Questions
- Table Maintenance Generator SAP ABAP INTERVIEW Que...
- TABStrips in ABAP SAP ABAP INTERVIEW Questions
- ABAP Tips and Tricks SAP ABAP INTERVIEW Questions
- Tree type report in ABAP SAP ABAP INTERVIEW Questions
- Explain Unicode-enabled ABAP program SAP ABAP INTE...
- Source Code Listing SAP ABAP INTERVIEW Questions
- What is User Exits and Customer Exits? SAP ABAP IN...
- A Short Tutorial on User Exits SAP ABAP INTERVIEW ...
- Program to Test Line Selection & Scrolling within ...
- Creating new program via ABAP SAP ABAP INTERVIEW Q...
- Program to Hide ABAP's Source Code and Protects it...
- Protect part of ABAP code from modifying SAP ABAP ...
- How to delete an editor lock? SAP ABAP INTERVIEW Q...
- Check Length and Alpha Numeric Variable SAP ABAP I...
- Run or Display ABAP Report over the web SAP ABAP I...
- Working on Polymorphism SAP ABAP INTERVIEW Questions
- How to Write Web Reports in SAP SAP ABAP INTERVIEW...
- Steps to Creating domains, Data Elements, Tables S...
- Function to Display All the Columns of any Table W...
- Difference between Work Area and Header Line SAP A...
- The Different Types of SAP Tables
- Difference between a check table and a value table...
- Quick Note on Design of secondary database indexes...
- How to get the field descriptions of a table? SAP ...
- What is use of using HASHED TABLE?
- How to create a Dynamic Internal Table or Array? S...
- Which table is the developer key stored in? SAP AB...
- What is use of using HASHED TABLE? SAP ABAP INTERV...
- Trace when a variant of a report was created SAP A...
- Difference between extract and collect statements ...
- What Are Different Types Of Internal Tables and Th...
- What is the Different Types and Usage of Views SAP...
- How Loop Works in Internal Tables
- Easy Way To Remember Table In SAP
- ► 03/16 - 03/23 (44)
- ► 02/24 - 03/02 (9)
- ► 02/17 - 02/24 (32)
No comments:
Post a Comment