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

How to search in internal table

Former Member
0 Likes
9,819

I am a newbie trying to search in an internal table. I tried the following, but I am getting an error message saying that it expected type "string" for the internal table:

DATA:

BEGIN OF WA_TAB1,

WA_COLUMN1 TYPE TAB2-COLUMN1,

WA_COLUMN2 TYPE TAB2-COLUMN2,

END OF WA_TAB1,

TBL_TAB1 LIKE TABLE OF WA_TAB1

WITH KEY WA_COLUMN1,

TBL_TAB1_RESULT TYPE MATCH_RESULT_TAB.

FIND ALL OCCURRENCES OF 'ABC'

IN TABLE TBL_TAB1

RESULTS TBL_TAB1_RESULT.

How can I do a search like this within an internal table (like SELECT in SQL)? I expect to get several rows back and I need the content of all columns for these rows.

Thanks for your help,

D.

1 ACCEPTED SOLUTION
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
3,210

It appears that the system rather have the fields of the internal table as character type as opposed to string typed. This seems to work for me.



REPORT  zrich_0002.

TYPES: BEGIN OF tab2,
       column1(20) TYPE c,
       column2(20) TYPE c,
       END OF tab2.


DATA: BEGIN OF wa_tab1,
      wa_column1 TYPE tab2-column1,
      wa_column2 TYPE tab2-column2,
      END OF wa_tab1,

      tbl_tab1 LIKE TABLE OF wa_tab1
                     WITH KEY wa_column1,

      tbl_tab1_result TYPE match_result_tab,
      wa_tab1_result LIKE LINE OF tbl_tab1_result.


wa_tab1-wa_column1 = 'Column 1'.
wa_tab1-wa_column2 = 'Column 2'.
APPEND wa_tab1 TO tbl_tab1.

wa_tab1-wa_column1 = 'This is ABC in this column'.
wa_tab1-wa_column2 = 'Column 2'.
APPEND wa_tab1 TO tbl_tab1.

wa_tab1-wa_column1 = 'Column 1'.
wa_tab1-wa_column2 = 'Column 2'.
APPEND wa_tab1 TO tbl_tab1.

wa_tab1-wa_column1 = 'Column 1'.
wa_tab1-wa_column2 = 'This is ABC in this column'.
APPEND wa_tab1 TO tbl_tab1.

FIND ALL OCCURRENCES OF 'ABC' IN TABLE tbl_tab1
              RESULTS tbl_tab1_result IN CHARACTER MODE.

LOOP AT tbl_tab1_result INTO wa_tab1_result.
  READ TABLE tbl_tab1 INTO wa_tab1 INDEX wa_tab1_result-line.
  IF sy-subrc = 0.
    WRITE:/ 'Line:', wa_tab1_result-line, 'Offset:', wa_tab1_result-offset.
    WRITE: 'Value is:',  wa_tab1+wa_tab1_result-offset(3).
  ENDIF.
ENDLOOP.

Regards,

Rich Heilman

Message was edited by:

Rich Heilman

I am a newbie trying to search in an internal table. I tried the following, but I am getting an error message saying that it expected type "string" for the internal table:

DATA:

BEGIN OF WA_TAB1,

WA_COLUMN1 TYPE TAB2-COLUMN1,

WA_COLUMN2 TYPE TAB2-COLUMN2,

END OF WA_TAB1,

TBL_TAB1 LIKE TABLE OF WA_TAB1

WITH KEY WA_COLUMN1,

TBL_TAB1_RESULT TYPE MATCH_RESULT_TAB.

FIND ALL OCCURRENCES OF 'ABC'

IN TABLE TBL_TAB1

RESULTS TBL_TAB1_RESULT.

How can I do a search like this within an internal table (like SELECT in SQL)? I expect to get several rows back and I need the content of all columns for these rows.

Thanks for your help,

D.

11 REPLIES 11
Read only

former_member192429
Active Participant
0 Likes
3,210

For picking Single record from internal table use READ TABLE ITAB index..

for multiple rows

LOOP at itab.

write the logic.

Endloop.

-Kriss

Read only

0 Likes
3,210

This sounds very inefficient for me. Is there no "Select" or "Find" statement I could use?

Read only

0 Likes
3,210

HI,

No Find will work with internal table of type string.. how ever you can loop and find.

loop at itab.

if itab-firstfield cp 'ABC' or itab-secondfiled cp 'ABC'.

resulttab = itab.

append resulttab.

endif.

endloop.

Thanks

Mahesh

Read only

Former Member
0 Likes
3,210

HI,

your intrnal table should be of type string.. check this example

DATA: itab TYPE TABLE OF string,

results TYPE match_result_tab.

...

FIND ALL OCCURRENCES OF REGEX '\b(Huey|Dewey|Louie)\b'

IN TABLE itab

RESPECTING CASE

RESULTS results.

Thanks

MaHESH

Read only

0 Likes
3,210

Mahesh,

I do not quite understand why I should have a table of type string (I saw that in the SAP help). The internal table I would like to search has a different structure with several columns of different types.

Does this mean I cannot use the "Find" statement?

Read only

Former Member
0 Likes
3,210

Hello

write something like:

option 1

read table TBL_TAB1 into wa_bla with table key wa_column = 'criteria'.

option 2

loop at TBL_TAB1.

code....

endloop.

Hope this helps

Gabriel

Read only

0 Likes
3,210

I guess I can use option 1 only if I expect no more than 1 result row, correct?

Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
3,211

It appears that the system rather have the fields of the internal table as character type as opposed to string typed. This seems to work for me.



REPORT  zrich_0002.

TYPES: BEGIN OF tab2,
       column1(20) TYPE c,
       column2(20) TYPE c,
       END OF tab2.


DATA: BEGIN OF wa_tab1,
      wa_column1 TYPE tab2-column1,
      wa_column2 TYPE tab2-column2,
      END OF wa_tab1,

      tbl_tab1 LIKE TABLE OF wa_tab1
                     WITH KEY wa_column1,

      tbl_tab1_result TYPE match_result_tab,
      wa_tab1_result LIKE LINE OF tbl_tab1_result.


wa_tab1-wa_column1 = 'Column 1'.
wa_tab1-wa_column2 = 'Column 2'.
APPEND wa_tab1 TO tbl_tab1.

wa_tab1-wa_column1 = 'This is ABC in this column'.
wa_tab1-wa_column2 = 'Column 2'.
APPEND wa_tab1 TO tbl_tab1.

wa_tab1-wa_column1 = 'Column 1'.
wa_tab1-wa_column2 = 'Column 2'.
APPEND wa_tab1 TO tbl_tab1.

wa_tab1-wa_column1 = 'Column 1'.
wa_tab1-wa_column2 = 'This is ABC in this column'.
APPEND wa_tab1 TO tbl_tab1.

FIND ALL OCCURRENCES OF 'ABC' IN TABLE tbl_tab1
              RESULTS tbl_tab1_result IN CHARACTER MODE.

LOOP AT tbl_tab1_result INTO wa_tab1_result.
  READ TABLE tbl_tab1 INTO wa_tab1 INDEX wa_tab1_result-line.
  IF sy-subrc = 0.
    WRITE:/ 'Line:', wa_tab1_result-line, 'Offset:', wa_tab1_result-offset.
    WRITE: 'Value is:',  wa_tab1+wa_tab1_result-offset(3).
  ENDIF.
ENDLOOP.

Regards,

Rich Heilman

Message was edited by:

Rich Heilman

Read only

0 Likes
3,210

Rich,

I think "FIND ALL OCCURRENCES" will not support.i checked the syntax for FIND

"FIND p IN [SECTION OFFSET off LENGTH len OF] text"

aRs

Read only

0 Likes
3,210

FIND ALL OCCURRENCES is new in BI 7.0. I am testing Rich's solution right now.

Thanks,

D.

Read only

0 Likes
3,210

That's right, I wrote that code in NetWeaver 7.0 system.

Regards,

Rich Heilman