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: 

Conditional Internal Table Assignment to Field-Symbol

Former Member
0 Kudos

Hi Experts,

I have a code which has 2 internal tables on which either one of them should be assigned to a field-symbol for the LOOP AT syntax below:

DATA: i_temp   TYPE STANDARD TABLE OF t_temp WITH HEADER LINE,
            i_temp_2 LIKE STANDARD TABLE OF i_mat_ser WITH HEADER LINE.

    IF i_temp[ ] IS NOT INITIAL.
      ASSIGN i_temp TO <fs_temp>.

      CREATE DATA v_dref LIKE LINE OF <fs_temp>.
      ASSIGN v_dref->* TO <fs_temp1>.
    ELSE.
      ASSIGN i_temp_2 TO <fs_temp>.

      CREATE DATA v_dref LIKE LINE OF <fs_temp>.
      ASSIGN v_dref->* TO <fs_temp1>.
    ENDIF.
    LOOP AT i_temp2.
    ...
    LOOP AT <fs_temp> ASSIGNING <fs_temp1>.
      IF     <fs_temp1>-matnr = i_temp2-matnr
         AND <fs_temp1>-werks = i_temp2-werks
         AND <fs_temp1>-lgort = i_temp2-lgort.

However, it gives me "I_TEMP[] and <FS_TEMP> are type-incompatible."

I also tried this code:

     DATA: c_temp(6) TYPE c "i_temp"
      CREATE DATA v_table LIKE c_temp.
      ASSIGN v_dref->* TO <fs_temp1>. 

But it does not assign the structure type of the table to the field-symbol. Any ideas on these where any internal table can be assigned to the field-symbol?

Edited by: Matt on May 9, 2011 8:57 AM - added tags

1 ACCEPTED SOLUTION

former_member186741
Active Contributor
0 Kudos

as Matt said, tables with headers should not be used, but in your original code you are using them and the problem is that the assign is with the table header and not the whole table:

IF i_temp[ ] IS NOT INITIAL.

ASSIGN i_temp TO <fs_temp>.

should be:

IF i_temp[ ] IS NOT INITIAL.

ASSIGN i_temp[] TO <fs_temp>.

18 REPLIES 18

matt
Active Contributor
0 Kudos

Get into the habit of using meaningful variable names - it will make life a lot easier for everyone. Don't use tables with header lines - it's bad programming practice. (Yes, I know it's taught in BC400 and is an integral to parts of ABAP - but avoid).

You need to say precisely which line in your code is generating the error. You've also not stated how you've declared the field-symbols. Which is probably where the issue lies.

Former Member
0 Kudos

Hii...

If you are using field symbol then you have to give some type.. for this reason.. you can give.. "TYPE ANY" for assignment.. so that.. field symbol will allocate memory at run time.. but.. it is mandatory to give.. some type.

So, better use.. "TYPE ANY" or "TYPE ANY TABLE".

Hope this will surely solve your problem..

Thanks

Jhings

Former Member
0 Kudos

You must define field symbol <FS_TEMP> of type t_temp and I_TEMP[] should be type t_temp to avoid type compatible error.

matt
Active Contributor
0 Kudos

You must define field symbol <FS_TEMP> of type t_temp and I_TEMP[] should be type t_temp to avoid type compatible error.

No he musn't. because if he does, the other table won't be compatible, will it?!

Former Member
0 Kudos

Hi,

Try assigning the index of the structure like :

assign component l_index of structure itab to <fs>.

regards,

Murali

matt
Active Contributor
0 Kudos

Hi,

>

> Try assigning the index of the structure like :

>

> assign component l_index of structure itab to <fs>.

>

>

> regards,

>

> Murali

And how would that help? Did you even understand the original question?

Former Member
0 Kudos

Hi,

DATA: c_temp(6) TYPE c "i_temp"

I guess the name of the variable must be in caps, please change the value of c_temp to "I_TEMP", this should be in caps and try.

Regards,

Chen

matt
Active Contributor
0 Kudos

Hi,

>

> DATA: c_temp(6) TYPE c "i_temp">

> I guess the name of the variable must be in caps, please change the value of c_temp to "I_TEMP", this should be in caps and try.

>

> Regards,

> Chen

In fact, it doesn't. Though I use caps, because it looks better to me. The syntax doesn't work anyway. The way the OP has written it, he'll end up with the field-symbol being CHAR 6. There is no syntax that's LIKE (c_temp).

The issue could stem from using tables with header line, and exemplifies why you shouldn't use them. The following should work: ASSIGN i_temp[] TO <fs_temp>.

Until the OP responds with accurate information, it's kind of difficult to provide useful answers.

0 Kudos

Sir, Thank you for your answer and the modification of the Moderator...I should have used Markup for the codes also.

For the question, I'm very sorry for the confusion but instead of the complicated combination of CREATE DATA...REF and ASSIGN statement, I have done only ASSIGN Statement, like below:


ASSIGN i_temp[ ] TO <fs_temp>.

This is for the condition on which structure should the field-symbol be assigned, as for the condition:


IF i_temp[] IS NOT INITIAL.
  ASSIGN i_temp[ ] TO <fs_temp>
ELSEif i_temp2[] IS NOT INITIAL.
  ASSIGN i_temp2[] TO <fs_temp>.

Where <fs_temp> is declared as TYPE STANDARD TABLE.

The Problem is when I start a loop of the <fs_temp>, which should be one of the structure of the internal tables.

Note that I already changed the declaraion of i_temp and i_temp to without the header-line, so as to increase efficiency.


CREATE DATA v_dref LIKE LINE OF <fs_temp>. " This for the work-area assigned to field-symbol
ASSIGN v_dref->* TO <fs_wa_temp>.

CLEAR wa_main.
READ TABLE i_main INTO wa_main INDEX 1. " This is just an example for the where condition
LOOP AT <fs_temp> ASSIGNING <fs_wa_temp> 
WHERE matnr = wa_main-matnr
             werks = wa_main-werks
             lgort    = wa_main-lgort.

Which is not working since matnr, werks and lgort cannot be seen as part of the structure of <fs_temp>.

How can we do a simple filter of values for matnr, werks and lgort inside the loop for this?

Former Member
0 Kudos

Hi,

Instead of assigning internal table to field symbol, better to assign to another internal table and use that internal table as per your requirement.

some times we will not allow to assign internal table to field symbols. or else we can use as below

field -symbols: <fs> type any table.

Ram.

former_member186741
Active Contributor
0 Kudos

as Matt said, tables with headers should not be used, but in your original code you are using them and the problem is that the assign is with the table header and not the whole table:

IF i_temp[ ] IS NOT INITIAL.

ASSIGN i_temp TO <fs_temp>.

should be:

IF i_temp[ ] IS NOT INITIAL.

ASSIGN i_temp[] TO <fs_temp>.

0 Kudos

Thank you for answering, I already did change the internal table declaration or i_temp and i_temp2 without headline. So I think this is not the problem. I am using now a separate work-area type line of the i_temp or i_temp2 as with the code below to catch the structure of either the two, depending on the condition:


    IF i_temp[] IS NOT INITIAL.
      ASSIGN i_temp[] TO <fs_temp>.
    ELSEIF i_temp2[] IS NOT INITIAL.
      ASSIGN i_temp2[] TO <fs_temp>.
    ENDIF.

    CREATE DATA v_dref LIKE LINE OF <fs_temp>.
    ASSIGN v_dref->* TO <fs_wa_temp>.

0 Kudos

so what is the new problem? A syntax error telling you that MATNR etc do not belong to the <temp> structure?

try something like:

field-symbols: <matnr>,<werks>,<lgort>.

LOOP AT <fs_temp> ASSIGNING <fs_wa_temp> .

assign component 'MATNR' OF <fs_wa_temp> TO <MATNR>.

CHECK SY-SUBRC = 0.

assign component 'WERKS' OF <fs_wa_temp> TO <WERKS>.

CHECK SY-SUBRC = 0.

assign component 'LGORT' OF <fs_wa_temp> TO <LGORT>.

CHECK SY-SUBRC = 0.

CHECK <matnr> = wa_main-matnr.

CHECK <werks> = wa_main-werks.

check <lgort> = wa_main-lgort.

0 Kudos

<fs_temp> is generically defined - so you cannot reference its components directly. You must use something like

FIELD-SYMBOLS: <matnr> TYPE MATNR.
...
ASSIGN COMPONENT 'MATNR' OF STRUCTURE <fs_temp> TO <matnr>

By the way - think about your naming. Do you really need to prefix field symbols with <fs...>. The fact that it is in angle brackets in the first place already means it must be a field symbol!

edit: Neil types faster than me. But I still recommend TYPING field symbols as tightly as possible. It makes for more robust programming - you can pick up certain errors at compile time rather than run time, which is more efficient and therefore cheaper.

matt

Edited by: Matt on May 10, 2011 7:20 AM

0 Kudos

Ok, I have compiled the syntax and it is working with the code you sugegsted below:


FIELD-SYMBOLS: <matnr> TYPE MATNR.
...
ASSIGN COMPONENT 'MATNR' OF STRUCTURE <fs_temp> TO <matnr>

for the fs-prefix, I just used the naming standard given to me, it makes more sense not to use it but I cannot change them as what they want me to follow originally and peer review my work will not go well.

But if you're concerned only as a syntax only for this thread, my bad, I'm only used to naming conventions given to me.

Thank you so much for your help!

0 Kudos

My two cents ...

LOOP AT <fs_temp> ASSIGNING <fs_wa_temp> 
WHERE matnr = wa_main-matnr
             werks = wa_main-werks
             lgort    = wa_main-lgort.

If you're on Release 702 you can use dynamic WHERE clause in the LOOP instead!

0 Kudos

> But if you're concerned only as a syntax only for this thread, my bad, I'm only used to naming conventions given to me.

>

> Thank you so much for your help!

You're welcome. It's not really a concern - it's an observation. I've seen it in naming conventions as well, but once you think about it, it makes no sense at all! I think it begins with when you're first introduced to field symbols, the examples use <fs>.

Former Member
0 Kudos

This message was moderated.