2013 Oct 10 12:46 PM
Hi All,
While developing a program, I felt need of recursion in my program. So I was thinking of writing a perform that will call itself with a parameter that will keep track of it from becoming a never-ending loop. e.g.
data : n type i value '04',
string_tab type string.
concatenate 'gt_tab' '01' into string_tab.
perform subroutine_name tables string_tab.
form subroutine_name tables p_table.
if n gt '01'.
"My logic
n = n - 1.
concatenate 'gt_tab' n into string_tab.
perform subroutine_name tables string_tab.
else.
exit.
endif.
endform.
However, I am unable to give a name of table to perform. My question is can we do that somehow as we did in select?
e.g.
data v_stringtab type string value 'MARA'.
select *
from (v_stringtab)
into table gt_mara.
Is it possible in SAP ABAP? Any Answer or hint is fine. I need some hint to proceed to right direction.
Hi All,
While developing a program, I felt need of recursion in my program. So I was thinking of writing a perform that will call itself with a parameter that will keep track of it from becoming a never-ending loop. e.g.
data : n type i value '04',
string_tab type string.
concatenate 'gt_tab' '01' into string_tab.
perform subroutine_name tables string_tab.
form subroutine_name tables p_table.
if n gt '01'.
"My logic
n = n - 1.
concatenate 'gt_tab' n into string_tab.
perform subroutine_name tables string_tab.
else.
exit.
endif.
endform.
However, I am unable to give a name of table to perform. My question is can we do that somehow as we did in select?
e.g.
data v_stringtab type string value 'MARA'.
select *
from (v_stringtab)
into table gt_mara.
Is it possible in SAP ABAP? Any Answer or hint is fine. I need some hint to proceed to right direction.
2013 Oct 10 12:51 PM
Dear, Declare your itab globally.
TYPES: itab1 TYPE STANDARD TABLE <TABLE_name>
PERFORM FORM_FILL CHANGING itab1.
FORM FORM_FILL CHANGING itab1 type <TABLE_name>.
****************Your Logic
ENDFORM.
Search on Google for the same. or look into the thread.. "" passing internal table into a subroutine..""
2013 Oct 10 12:55 PM
Hi Preetam,
You can use TABLE
Perform select_details using lx_final lv_colu changing lt_pernr.
Form select_details using px_final type zst_manpower
pv_colu type i
changing pt_pernr type table.
Endform
SuDEESH
2013 Oct 10 1:09 PM
Hi,
First Declare the table as TYPE STANDARD TABLE.
Then call the Form,
PERFORM saubroutine_name TABLES itab.
form subroutine_name tables p_table like itab[].
<logic>.
endform.
2013 Oct 10 1:16 PM
@All: I think I was not clear enough for all of you.
Let's say my table name keeps changing, then what.
I want same perform to recall itself with different table name as per the situation.
e.g.
perform subroutine_name tables itab1.
form subroutine_name tables p_table.
perform subroutine_name tables itab2.
endform.
itab1 and itab2 both have same structure. however, i want to use a variable that can pass the name of table to the perform. Is that possible?
2013 Oct 10 1:18 PM
If it is dynamic.. Just mention TYPE TABLE.
Eg..
form select_details using px_final type zst_manpower
pv_colu TYPE I
changing pt_pernr type table.
This will work! But I'm using the same.
SuDEESH
2013 Oct 10 1:20 PM
@Sudeesh: Type of table is not dynamic. Name of table is dynamic. like... itab1, itab2, itab3.
All of three tables are of same structure.
2013 Oct 10 1:22 PM
No, i don't think so. because in
perform subroutine_name tables itab1.
form subroutine_name tables p_table.
You are using Tables means it will look for a table(internal table) not for a variable..
2013 Oct 10 1:24 PM
@Chandra: Can I use using or changing for internal tables and pass names to perform?
2013 Oct 10 1:30 PM
Try this code..
DATA : BEGIN OF LINE OCCURS 0,
LINE1 TYPE I,
LINE2 TYPE I,
LINE3 TYPE I,
END OF LINE.
DATA : INT_NAME(10) TYPE C,
FUN_NAME(10) TYPE C,
SORT1(10) TYPE C,
SORT2(10) TYPE C.
LINE-LINE1 = 1.
LINE-LINE2 = 10.
LINE-LINE3 = 100.
APPEND LINE.
LINE-LINE1 = 2.
LINE-LINE2 = 20.
LINE-LINE3 = 200.
APPEND LINE.
LINE-LINE1 = 3.
LINE-LINE2 = 30.
LINE-LINE3 = 300.
APPEND LINE.
LINE-LINE1 = 4.
LINE-LINE2 = 40.
LINE-LINE3 = 400.
APPEND LINE.
* This is the internal table name passing as a parameter.
INT_NAME = 'line'.
PERFORM COMPUTE_IT USING (INT_NAME) FUN_NAME SORT1 SORT2.
*---------------------------------------------------------------------*
* FORM compute_it *
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
* --> PINT_NAME *
* --> PFUN_NAME *
* --> PSORT1 *
* --> PSORT2 *
*---------------------------------------------------------------------*
FORM COMPUTE_IT USING (PINT_NAME) LIKE INT_NAME PFUN_NAME PSORT1
PSORT2 .
WRITE : PINT_NAME.
LOOP AT PINT_NAME.
WRITE: LINE-LINE1.
WRITE: LINE-LINE2.
WRITE: LINE-LINE3.
ENDLOOP.
ENDFORM.
2013 Oct 10 1:32 PM
Sorry Preetam.. I misunderstood your requirement.
You can pass like this
SuDEESH
2013 Oct 10 1:38 PM
It is better to use changing..
http://help.sap.com/saphelp_nw2004s/helpdata/en/9f/db979d35c111d1829f0000e829fbfe/content.htm
2013 Oct 17 2:53 PM
2013 Oct 17 2:53 PM
2013 Oct 10 1:44 PM
This should work (if I understood you correctly) :
FIELD-SYMBOLS: <fs_structure> TYPE ANY TABLE.
Data: lv_ref TYPE REF TO data,
lv_table type string.
lv_table = 'MARA'.
CREATE DATA lv_ref TYPE table of (lv_table).
ASSIGN lv_ref->* TO <fs_structure>.
select * from (lv_table)
into TABLE <fs_structure>.
2013 Oct 10 2:02 PM
Hi Preetam,
Yesterday i published a blog about executing sql statements directly in sap.
i use dynamic program generation and dynamic table there.
You can check code.
https://scn.sap.com/community/abap/blog/2013/10/09/zsql-a-tool-to-execute-sql-statements-directly
Regards
Basar Ozgur
2013 Oct 10 2:30 PM
This will work, but only if the table name that is constructed dynamically exists in the dictionary.
I used HR tables for this example
DATA: dyn_name TYPE tabname,
part_name TYPE tabname,
counter TYPE numc2.
part_name = 'HRP10'.
CONCATENATE part_name counter INTO dyn_name.
PERFORM dosomething CHANGING dyn_name
part_name
counter.
FORM dosomething CHANGING p_dyn_name TYPE tabname
p_part_name TYPE tabname
p_counter TYPE numc2.
FIELD-SYMBOLS: <wa> TYPE ANY,
<table> TYPE ANY TABLE.
DATA: wa_ref TYPE REF TO data,
tab_ref TYPE REF TO data.
p_counter = p_counter + 1.
WHILE p_counter < 10.
CREATE DATA wa_ref TYPE (p_dyn_name).
CREATE DATA tab_ref TYPE TABLE OF (p_dyn_name).
ASSIGN wa_ref->* TO <wa>.
ASSIGN tab_ref->* TO <table>.
SELECT * FROM (p_dyn_name) INTO TABLE <table>.
LOOP AT <table> ASSIGNING <wa>.
ENDLOOP.
CONCATENATE p_part_name p_counter INTO p_dyn_name.
PERFORM dosomething CHANGING p_dyn_name
p_part_name
p_counter.
ENDWHILE.
ENDFORM. " DOSOMETHING
2013 Oct 17 2:54 PM
2014 Mar 31 9:22 AM
Hi Preetam,
I've used references (pointers) instead of normal parameters with predefined types.
REPORT ztesla.
"----------------------------------------
DATA: gt_tm1 TYPE STANDARD TABLE OF mara,
gv_ref TYPE REF TO data.
"----------------------------------------
START-OF-SELECTION.
SELECT * INTO TABLE gt_tm1 FROM mara UP TO 7 ROWS.
GET REFERENCE OF gt_tm1 INTO gv_ref.
PERFORM f_doit USING gv_ref '7'.
*&------------------------------------------*
*& Form f_doit
*&------------------------------------------*
FORM f_doit USING p_ref TYPE REF TO data
p_option TYPE char02.
"········································
DATA: lv_ref TYPE REF TO data,
lt_mara TYPE STANDARD TABLE OF mara.
"········································
FIELD-SYMBOLS: <lfs_tmara> TYPE ANY TABLE.
ASSIGN p_ref->* TO <lfs_tmara>.
"········································
CASE p_option.
WHEN '7'.
REFRESH lt_mara. SELECT * INTO TABLE lt_mara FROM mara UP TO 10 ROWS.
GET REFERENCE OF lt_mara INTO lv_ref.
PERFORM f_doit USING lv_ref '99'.
WHEN '99'.
PERFORM f_show_alv USING p_ref.
WHEN OTHERS.
ENDCASE.
"········································
" You may use the next sentence to get/modify the data: (*)
" assign component of + field-symbols (*)
ENDFORM. "f_doit
*&------------------------------------------*
*& Form f_show_alv
*&------------------------------------------*
FORM f_show_alv USING p_table_ref TYPE any.
DATA: lo_alv TYPE REF TO cl_salv_table.
FIELD-SYMBOLS: <lfs_table> TYPE ANY TABLE.
ASSIGN p_table_ref->* TO <lfs_table>.
TRY.
CALL METHOD cl_salv_table=>factory
IMPORTING
r_salv_table = lo_alv
CHANGING
t_table = <lfs_table>.
CATCH cx_salv_msg.
ENDTRY.
lo_alv->display( ).
ENDFORM. "f_show_alv
(*)
FIELD-SYMBOLS: <lfs_field> type any,
<lfs_struct> type any.
* assign ... to <lfs_struct>
ASSIGN COMPONENT 'MATNR' of STRUCTURE <lfs_struct> to <lfs_field>.
<lfs_field> = ...
Regards,
Luis
| User | Count |
|---|---|
| 3 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |