Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Retaining called program values in the calling program.

Former Member
0 Kudos
240

Hi

Suppose im coding in progam A and I call program B. Is there any way in which i can access the variables/values of program B in program A?.

Also is there any way in which program B can access program A values.

1 ACCEPTED SOLUTION

former_member195383
Active Contributor
0 Kudos
87

make use of import / export statements...

U can export a variable in program B and then can import the same in Program A.The reverse can also be done...

10 REPLIES 10

former_member195383
Active Contributor
0 Kudos
88

make use of import / export statements...

U can export a variable in program B and then can import the same in Program A.The reverse can also be done...

bpawanchand
Active Contributor
0 Kudos
87

Hi

Welcome to SDN

Yes you can access one program variables into another by using IMPORT and EXPORT statements where your ABAP programs will access the memory to access those values

For further information about IMPORT and EXPORT statements

http://www.geocities.com/rmtiwari/Resources/Utilities/ABAPReference/ABAPReference.html

Regards

Pavan

Former Member
0 Kudos
87

Hi,

For that you have use SAP Memory.

Suppose In Program A You have Variable var1.

Now if you want to access the value of Var1 in Program B then,

Set the Parameter Id for Var1.

Like-

SET PARAMETER ID 'P001' FIELD var1.

Now you want the value of var1 into Program b the,

use - GET PARAMETER ID 'P001' field var2.

Var2 is the variable of Program B.

Same comdition will there when you access the variable of Program B into Program A.

You can also use EXPORT And IMPORT statement if the Both Program are opened in same External Session.

Like

EXPORT var1 TO MEMORY ID 'P001'.

For Getting value-

IMPORT var1 to Var2 FROM MEMORY ID 'P001'.

Hope it will help you.

Regards,

Sujit

Former Member
0 Kudos
87

Hii!

Use Import/ Export statements.

Check out this sample code, actually they are two programs.

In program1, I'm fetching the data from spfli table. In program2, I'm fetching the data from sflight corresponding to records from spfli.


REPORT  Z_abap_memory.
DATA:
  w_carrid TYPE spfli-carrid,
  BEGIN OF fs_spfli,
    carrid LIKE spfli-carrid,
    connid LIKE spfli-connid,
    fltime LIKE spfli-fltime,
  END OF fs_spfli.
DATA:
  t_spfli LIKE
    TABLE OF
          fs_spfli.

SELECT-OPTIONS:
  s_carrid FOR w_carrid.

START-OF-SELECTION.
  PERFORM get_spfli.
  PERFORM disp_spfli.
AT LINE-SELECTION.
  IF sy-lsind EQ 1.
    EXPORT t_spfli TO MEMORY ID 'ABC'.
    SUBMIT Z_ABAP_MEMORY1 AND RETURN.
  ENDIF.
END-OF-SELECTION.
    PERFORM disp_spfli.
*&---------------------------------------------------------------------*
*&      Form  get_spfli
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM get_spfli .
  SELECT carrid
         connid
         fltime
    FROM spfli
    INTO TABLE t_spfli
   WHERE carrid IN s_carrid.

ENDFORM.                    " get_spfli
*&---------------------------------------------------------------------
*
*&      Form  disp_spfli
*&---------------------------------------------------------------------
*
*       text
*----------------------------------------------------------------------
*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------
*
FORM disp_spfli .
  LOOP AT t_spfli INTO fs_spfli.
    WRITE: / fs_spfli-carrid,
             fs_spfli-connid,
             fs_spfli-fltime.
  ENDLOOP.
  HIDE:
    fs_spfli-carrid,
    fs_spfli-connid.
ENDFORM.                    " disp_spfli

PROGRAM 2


REPORT  Z_abap_memory1.
DATA:
  BEGIN OF fs_spfli,
    carrid LIKE spfli-carrid,
    connid LIKE spfli-connid,
    fltime LIKE spfli-fltime,
  END OF fs_spfli,
  fs_fl LIKE fs_spfli.
DATA:
  BEGIN OF fs_flight,
    carrid LIKE sflight-carrid,
    connid LIKE sflight-connid,
    fldate LIKE sflight-fldate,
  END OF fs_flight.

DATA:
  t_spfli LIKE
    TABLE OF
          fs_spfli.

DATA:
  t_fl LIKE t_spfli.

DATA:
  t_flight LIKE
     TABLE OF
           fs_flight.

IMPORT t_spfli FROM MEMORY ID 'ABC'.
t_fl = t_spfli.

SELECT carrid
       connid
       fldate
  FROM sflight
  INTO TABLE t_flight
  FOR ALL ENTRIES IN t_spfli
 WHERE carrid = t_spfli-carrid
   AND connid = t_spfli-connid.


LOOP AT t_flight INTO fs_flight.
  WRITE: / fs_flight-carrid,
           fs_flight-connid,
           fs_flight-fldate.
ENDLOOP.

Regards

Abhijeet

Former Member
0 Kudos
87

Not only variables but I would also want to have the same internal table in the 2 programs.

Can you give me the exact syntax for this parameter ID?.

The thing is im Dynamically creating program B in program A and then calling that program B.

If i don't have access to program B's variablesin program A and vice versa then I would have to have the remainder of my code in program B(ie in the internal table which contains my code for program B).

Former Member
0 Kudos
87

Thanks everyone.

Will try out this logic now.

Will definitely reward you'll if it works.

Once again thanks a ton.

Former Member
0 Kudos
87

Hi Abhijeet

When i export a variable to another program and that program makes changes to the variable are the changes visible in the calling program?.

0 Kudos
87

Hello Dexter,

The example given above with IMPORT and EXPORT are good. And don't use the SAP memory for your local programs. Not a good practice.

One important thing from your post is that, you have given it as..

When i export a variable to another program and that program makes changes to the variable are the changes visible in the calling program?.

While importing or exporting, please remember it that you are not importing to or exporting from any other program. It is the ABAP memory that you are using which is shared by your internal sessions. When you import to a memory location, that value is stored in the ABAP memory and when you call another program using SUBMIT, the other program is loaded into the internal session of your calling program. The memory used by both the programs is same thereby if you make any changes, they are also reflected into your calling program.

Regards

Indu.

0 Kudos
87

Hii Dexter!

If you pass a value from Program A to Program B and do some modifications then that modification will not be reflected back to calling program.

I have made two other simple programs, you can check them out.


REPORT z_sdn.

PARAMETERS:
  p_num TYPE i.

DATA: w_num TYPE i.


 w_num = p_num.

 EXPORT w_num TO MEMORY ID 'ABC'.

 SUBMIT z_sdn1 AND RETURN.

 WRITE: w_num.

Program 2


REPORT  Z_SDN1                                                      .

DATA: w_num TYPE i.

IMPORT w_num FROM MEMORY ID 'ABC'.

w_num = w_num + 2.
WRITE: w_num.

After executing the progam you'll get output as 5 , but when you click 'back' you'll get 3. This means that changes are not reflected back.

I hope this sample code will clear your doubt.

Regards

Abhijeet

Former Member
0 Kudos
87

Hi dexter,

1. U want the value of some variable xyz

from program 1,

into program 2.

2. We can use of EXPORT/IMPORT syntax.

3. Just make sure of the following things.

4. a) the variable name should be same

(in prog1, prog2)

b) The Declaration should also match

c) use export / import with the same name as variable

In prog1:


eg. DATA : MYVAR TYPE I.
EXPORT MYVAR TO MEMORY ID 'MYVAR'

In prog2:


eg. DATA : MYVAR TYPE I.
IMPORT MYVAR FROM MEMORY ID 'MYVAR'

5. Then it will work fine.

6. Moreover, u can check the value of

sy-subrc just after export/import

to know, whether it was successful or not.

regards,

amit m.