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: 

ABAP 7.40: Add columns after Inline declaration of an Internal Table

srajkoppolu
Explorer
0 Kudos

Hello,

Would it be possible to add an additional column after an internal table has been declared inline?

Eg., In order to find the transport which has a particular table entry, I would like to add an additional column TABKEY similar to E071K-TABKEY, and later on add the Transport owner and Description too.

SELECT * FROM T184 INTO TABLE @DATA(lt_t184) WHERE AUART = 'AA'.
IF sy-subrc = 0. " Add an additional column TABKEY to lt_t184 ENDIF.


or
Would I be able to declare an additional random field (not related to the tables involved) in a select statement?

SELECT T~*, random_field type tabkey as tabkey 
FROM T184 AS T INTO TABLE @DATA(lt_t184) WHERE AUART = 'AA'.

Thank you in advance.

1 ACCEPTED SOLUTION

Sandra_Rossi
Active Contributor

UPDATE: CAST is available since 7.40 SP5 but was very limited; it can be really fully used only since 7.50 (cf comments).

Since 7.50, you may use CAST to type according to a Predefined DDIC type:

SELECT T~*, CAST( ' ' AS CHAR(120) ) AS random_field
FROM T184 AS T
WHERE AUART = 'AA'
INTO TABLE @DATA(lt_t184).

or a host expression to type according to a DDIC data element (7.50 also):

SELECT T~*, @( VALUE tabkey( ) ) AS random_field
FROM T184 AS T
WHERE AUART = 'AA'
INTO TABLE @DATA(lt_t184).
7 REPLIES 7

Sandra_Rossi
Active Contributor

UPDATE: CAST is available since 7.40 SP5 but was very limited; it can be really fully used only since 7.50 (cf comments).

Since 7.50, you may use CAST to type according to a Predefined DDIC type:

SELECT T~*, CAST( ' ' AS CHAR(120) ) AS random_field
FROM T184 AS T
WHERE AUART = 'AA'
INTO TABLE @DATA(lt_t184).

or a host expression to type according to a DDIC data element (7.50 also):

SELECT T~*, @( VALUE tabkey( ) ) AS random_field
FROM T184 AS T
WHERE AUART = 'AA'
INTO TABLE @DATA(lt_t184).

Hello Sandra,

Thank you for your response. That is a great solution and would definitely have resolved my issue. However, unfortunately, the options listed in the link seem to be for 7.53 and are not available in our system as we are still on 7.40, and the only option we have is for data type fltp (7.40 CAST).

Thanks again for the answer.
Regards,
S.R.K

Too bad. Seems that the other types are available only since ABAP 7.50.

In 7.40, this should work:

SELECT T~*, 
"  1234567 10 234567 20 234567 30 234567 40 (number of characters)
  '                                        '
& '                                        '
& '                                        '
AS random_field
FROM T184 AS T
WHERE AUART = 'AA'
INTO TABLE @DATA(lt_t184).

(use 120 space characters to correspond to the DDIC type TABKEY; & is the literal operator, it's not part of ABAP SQL but part of ABAP, I use it here to make it more obvious how many space characters are here, i.e. 3 * 40)

PS: I realized that my first answer for ABAP >= 7.50 was incorrect (you can't do CAST( operand AS ddic_type)) so I edited it.

This solution above with 120 space character literal works for me.

I've tried other variations based on your example and the following work as well in 7.40.


Variation 1:

CONSTANTS: co_tabkey TYPE trobj_name VALUE ' '.
SELECT T~*,
       CASE t~mtext
         WHEN ' ' THEN @co_tabkey
         ELSE @co_tabkey
       END AS random_field
       FROM t000 AS t
       INTO TABLE @DATA(lt_t000).

Variation 2:

CONSTANTS: co_tabkey TYPE trobj_name VALUE ' '.
SELECT T~*, @co_tabkey AS random_field
       FROM t000 AS t
       INTO TABLE @DATA(lt_t000).

Thanks again for your response, Sandra.

Regards,

S.R.K

S. R. Koppolu Thanks for the feedback. Your comment deserves to be posted as an answer so that to gain visibility to future visitors, and you can switch the "best answer" to yours!

Föß
Active Participant
0 Kudos

Hi,

this solution should also work for older systems like 7.40 SP8.

lg Johann

REPORT zyfoes01.

DATA data_ref TYPE REF TO data.
DATA table_ref TYPE REF TO cl_abap_tabledescr.
DATA stru_ref TYPE REF TO cl_abap_structdescr.
DATA elem_ref TYPE REF TO cl_abap_elemdescr.
DATA components TYPE cl_abap_structdescr=>component_table.
DATA component TYPE LINE OF cl_abap_structdescr=>component_table.
DATA result2_ref TYPE REF TO data.

SELECT a~partner AS partner_number,
       a~type,
       b~addrnumber
  FROM but000 AS a
  INNER JOIN but020 AS b
     ON b~partner = a~partner
  INTO TABLE @DATA(result).

" get table details
GET REFERENCE OF result INTO data_ref.
table_ref ?= cl_abap_tabledescr=>describe_by_data_ref( data_ref ).
stru_ref ?= table_ref->get_table_line_type( ).
components = stru_ref->get_components( ).

" create new component based on dataelement MATNR
component-name = 'MATNR'.
component-type ?= cl_abap_elemdescr=>describe_by_name( 'MATNR' ).
APPEND component TO components.

" create new structure & table
stru_ref ?= cl_abap_structdescr=>create( p_components = components ).
table_ref ?= cl_abap_tabledescr=>create( p_line_type = stru_ref ).
CREATE DATA result2_ref TYPE HANDLE table_ref.

" assign reference and move data from result to result2.
ASSIGN result2_ref->* TO FIELD-SYMBOL(<result2>).

"<result2> = CORRESPONDING #( result ). ">7.50
MOVE-CORRESPONDING result TO <result2>.

0 Kudos

Hello Johann,

Thank you for your response. Your solution works as well for me in ABAP 7.40.

However, if possible, I was hoping to achieve this through the Inline declaration. Also, this solution utilizes additional memory as well.

But I do see the merits of this approach if the field to be selected is not known at the point of time the select occurs for instance condition record (Axxx) tables.

Regards,
S.R.K