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

Searching a dynamic table

Former Member
0 Likes
1,863

Hi,

I have a dynamic table <table> and want to find select all matchings with the value <value>.

To better understand something like this.

loop at <table> assigning <wa>.

" find <value> in <wa>.

endloop.

if found <value> should have the same type and value in the matching component in wa.

Please help. I have no clue.

Any suggestion is welcome.

thanks

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,732

Hi Maria,

If you know the field name its very simple

In LOOP Itself you can do this

DATA fieldname(20) TYPE c VALUE 'field'

LOOP AT <table> ASSIGNING <wa> where <table>-(fieldname) = <value>.

"Do the Required Operation

ENDLOOP. to compare

Here the variable fieldname should contain the name of your field which you want to compare

Thanks and Regards,

Pratheek

Hi,

I have a dynamic table <table> and want to find select all matchings with the value <value>.

To better understand something like this.

loop at <table> assigning <wa>.

" find <value> in <wa>.

endloop.

if found <value> should have the same type and value in the matching component in wa.

Please help. I have no clue.

Any suggestion is welcome.

thanks

15 REPLIES 15
Read only

matt
Active Contributor
0 Likes
1,732

There are a few approaches.

If you know the name of some of the fields, you can create a type with those fields. Then you can use.

FIELD-SYMBOLS: <mytab> TYPE ANY TABLE,

                <mywa>  TYPE ANY.

DATA: myfields TYPE the_type_with_the_fields.

LOOP AT <mytab> ASSIGNING <mywa>.

  MOVE-CORRESPONDING <mywa> INTO myfields.

" If you need to update the record with new values held in myfields.

  MOVE-CORRESPONDING myfields INTO <mywa>.

ENDLOOP.

Alternatively, you can use this:

FIELD-SYMBOLS: <mytab> TYPE ANY TABLE,

               <mywa>    TYPE ANY,

                <field1> TYPE ANY. " You should type it if you know what the type will be

DATA: r_data TYPE REF TO DATA.

CREATE DATA r_data LIKE LINE OF <mytab>. " Create area in memory with same shape as a record of <mytab>

ASSIGN r_data->* TO <mywa>. " Link that area to <mywa>

ASSIGN COMPONENT "fieldname" OF STRUCTURE <mywa> TO <field1>. "Instead of the fieldname, you could use a variable.

LOOP AT <mytab> INTO <mywa>.

" You can now manipulate the fields of <mywa> via <field1> etc.

" If you need to update the record

MODIFY TABLE <mytab> FROM <mywa>.

ENDLOOP.

If you don't know the names of the fields, and you can't derive them, you can use ASSIGN COMPONENT position OF STRUCTURE... where position is the number of the field in the structure - so 1 for the first etc.


Read only

Former Member
0 Likes
1,733

Hi Maria,

If you know the field name its very simple

In LOOP Itself you can do this

DATA fieldname(20) TYPE c VALUE 'field'

LOOP AT <table> ASSIGNING <wa> where <table>-(fieldname) = <value>.

"Do the Required Operation

ENDLOOP. to compare

Here the variable fieldname should contain the name of your field which you want to compare

Thanks and Regards,

Pratheek

Read only

matt
Active Contributor
0 Likes
1,732

LOOP AT WHERE with a dynamic where clause is not available in lower releases of ABAP. Also, the syntax would be LOOP AT ... ASSIGNING .... WHERE (fieldname) EQ <value>.

Read only

0 Likes
1,732

Hi Matthew,

Oh yes thanks for pointing out the mistake.

Thanks and Regards

Pratheek

Read only

0 Likes
1,732

Thank you for your answer.

I still have a problem because I dont know the field name. I am also getting a synthax error when I use "LOOP ....WHERE". It says that the tables line must be static.

I am getting the fields to search from another dynamic table.

LOOP AT <searchfieldtable> ASSIGNING <searchfield>.

        LOOP AT <table> ASSIGNING <wa> where <table>-???? = <searchfield>.

                 Append <wa> to <resulttable>.

       ENDLOOP.

ENDLOOP.

I know the get the type and the content of <searchfield> but I dont have a fields name.

The field found in <table> should have the same content and type than <searchfield>.

More like this:

LOOP AT <searchfieldtable> ASSIGNING <searchfield>.

        LOOP AT <table> ASSIGNING <wa> .

                

                 " if <searchfield> is found in <wa> then (I dont know how to do the search in <wa> without knowing the fieldname)

                 Append <wa> to <resulttable>.

       ENDLOOP.

ENDLOOP.

Please give me your suggestions.

tabl

Read only

matt
Active Contributor
0 Likes
1,732

So <searchfieldtable> contains a list of field names in <table>? That's easy then. Just do what I said above with ASSIGN COMPONENT...

LOOP AT <searchfieldtable> ASSIGNING <searchfield>.

  LOOP AT <table> ASSIGNING <wa> .

     ASSIGN COMPONENT <searchfield> OF STRUCTURE <wa> TO <foundfield>.

    CHECK sy-subrc IS INITIAL. "If it is, the field named in <searchfield> exists in wa.

    ...do stuff...

  ENDLOOP.

ENDLOOP.

Even if this isn't your specific scenario, somewhere the fieldnames must be being defined. If they're in the data dictionary, then you just look in table DD03L for the field names, for example.

Maybe you could give some examples of what these field symbols might contain at runttime, since this is what is critical.

Read only

0 Likes
1,732

I have created the <searchfield> and assigned it a type and a value. There should be a field in the workarea <wa> with this same type and value.


<searchfield> have a field name. I have created it with CREATE DATA... TYPE... and then assigned a value.

May be I should go through all components of
<wa> and compare their type and value with the type and value of
<searchfield>.

I dont have any other clue.


Read only

0 Likes
1,732

Hi Maria,

Does Search Field contain Field Name? or is it just the value?

From what i understand, it is a Value and not field name. That doesn't make any sense why would you search like that? you should have a field name to compare with, cross check whether the <searchfieldtable> contains field name.

Now if what i understood is correct I think you can solve it. So as you said if you know to get type from <searchfield>, i can explain you the procedure. Do as follows

LOOP AT <searchfieldtable> ASSIGNING <searchfield>.

"Get the type of <searchfield> and store it in lv_searchfieldtype

        LOOP AT <table> ASSIGNING <wa> .               

    do.

      ASSIGN COMPONENT sy-index OF STRUCTURE <wa> TO <fs_field>.

      IF <fs_field> IS ASSIGNED.

               "  Now here Get the type of <fs_field> store it in lv_fieldtype

               IF lv_searchfieldtype = lv_fieldtype AND <searchfield> = <fs_field>.

                           Append <wa> to <resulttable>.

                   ENDIF

      ELSE.

        exit.

      endif.

      UNASSIGN <fs_field>.

    enddo.

       ENDLOOP.

ENDLOOP.

I don't recommend this method as this contains Loop inside Loop and will degrade performance, Check if there is any way to get the field name, that would be a better way to do this.

Thanks and Regards,

Pratheek


Read only

matt
Active Contributor
0 Likes
1,732

I'm beginning to suspect that perhaps you're going about things the wrong way. Rather than tell us the technical stuff, what exactly are you trying to achieve? What are the inputs, what are the outputs? Perhaps then we can suggest a good, efficient, simple solution.

Often, if things start to get bogged down, you have to approach the problem from a different place.

regards

matt

Read only

0 Likes
1,732

Hi,

My goal is to search a table and give the result as a table only with line that contains certain(s) value(s).

I have received a field symbol table to search in. I have its structure ( different data types, not only CHAR). I get the values to search from another table and i am storing them in a field symbol table. The searchs values can be from different data types.

Regards.

Read only

0 Likes
1,732

I totally agree with you Sir.

Maria let us know what is the requirement, Usually a such a condition never occurs where you search without knowing the field name.

Thanks and Regards,

Pratheek

Read only

matt
Active Contributor
0 Likes
1,732

So why do you need the field names? Also, somewhere the table structure must be being defined, so at some point the actual fields names are known. For example, one solution may be to pass the field names as well as the data in the tables.

How is that you have the data types? Where are these coming from?

I'm honestly trying to help, but really you're not giving enough information. I've been handling dynamic tables for years and have never had a problem with them I couldn't solve.

Read only

0 Likes
1,732

Hi Matt,

I just found out that I can also extract the fieldname of the searchfield from the table where I got the value and the type.

Still the Loop at ... assigning .... where   is not working. Syntax error: line type should be static.

Regards,

Maria

Read only

matt
Active Contributor
0 Likes
1,732

Yes, because as already established, WHERE does not allow dynamic conditions. (Except possibly in the most recent ABAP release).

What is your desired WHERE clause. I'm still not clear on that.

Anyway, you have to use something like:

ASSIGN COMPONENT fieldname OF STRUCTURE <wa> TO <field>.

LOOP AT <table> INTO <wa>.

  CHECK <field> EQ somevalue.


Read only

0 Likes
1,732

Hi all,

I used in my loop:

ASSIGN COMPONENT fieldname OF STRUCTURE <wa> TO <field>.

and it worked.

Thank you all for your support.

Best regards.