Application Development and Automation 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: 
Read only

dynamic declaration for variable.

Former Member
0 Likes
895

Can we declare a variable dynamic with varing data type.

My req is i have to define a variable to store output from a dynamic select statement. Now since SELECT is dynamic, its output type changes each time and for the same I need a variable defined dynamically.

Regards,

Arpit

4 REPLIES 4
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
681

You may be able to use field symbols for this. Do you have experience with field symbols? Here is an example.

report zrich_0002.


data: matnr type mara-matnr.
data: ersda type mara-ersda.
data: brgew type mara-brgew.
data: field(20) type c.

field-symbols: <fs>.


parameters: p_matnr type mara-matnr.

start-of-selection.

  assign matnr to <fs>.
  select single matnr into <fs> from mara
               where matnr = p_matnr.
  write:/ <fs>.


  assign ersda to <fs>.
  select single ersda into <fs> from mara
               where matnr = p_matnr.
  write:/ <fs>.

  assign brgew to <fs>.
  select single brgew into <fs> from mara
               where matnr = p_matnr.
  write:/ <fs>.

Regards,

Rich Heilman

Read only

0 Likes
681

Thanks Rich,

I did the same thing but it is giving problem with data type DEC. Following is the error from dump.

Since the caller of the procedure could not have expected this exception

to occur, the running program was terminated.

The reason for the exception is:

The data read during a SELECT access could not be inserted into the

target field.

Either conversion is not supported for the target field's type or the

target field is too short to accept the value or the data are not in a

form that the target field can accept

Regards,

Arpit

Message was edited by: Arpit Gattani

Read only

0 Likes
681

Define the catching field as....

data: the_field(13) type p decimals 3.

Does it work now?

Regards,

Rich Heilman

Read only

Former Member
0 Likes
681

Hi Arpit Gattani,

To store output of an dynamic Select statement you can use dynamic data objects and field symbols.

To create a data object dynamically during a program, you need a data reference variable and the following statement:

CREATE DATA <dref> TYPE <type>|LIKE <obj>.

This statement creates a data object in the internal session of the current ABAP program. After the statement, the data reference in the data reference variable <dref> points to the object. The data object that you create does not have its own name. You can only address it using a data reference variable. To access the contents of the data object, you must dereference the data reference.

You must specify the data type of the object using the TYPE or LIKE addition. In the TYPE addition, you can specify the data type dynamically as the contents of a field (this is not possible with other uses of TYPE).

CREATE DATA <dref> TYPE (<name>).

Here, <name> is the name of a field that contains the name of the required data type.

<b>Example:</b>

A specific field is read from database table X031L. Neither the field name nor the table name is known until runtime:

  • Read a field from the table X031L

PARAMETERS:

TAB_NAME LIKE SY-TNAME, "Table name

TAB_COMP LIKE X031L-FIELDNAME, "Field name

ANZAHL TYPE I DEFAULT 10. Number of lines

FIELD-SYMBOLS: <WA> TYPE ANY,

<COMP> TYPE ANY.

DATA: WA_REF TYPE REF TO DATA.

CREATE DATA WA_REF TYPE (TAB_NAME). "Suitable work area

ASSIGN WA_REF->* TO <WA>.

SELECT * FROM (TAB_NAME) INTO <WA>

UP TO anzahl ROWS.

WRITE: / TAB_COMP, <COMP>.

ENDSELECT.

Dont Forget to give points if it helps ;>)

Regards

Rakesh.