1

I am trying to search 8 tables and check if a certain value is present. I searched around a lot an I assume I have to work with field-symbols and dynamic statements. This is done in a report (executable program). So far I have my internal table, filled with 8 table names of the table that have to be searched:

BEGIN OF lt_tables_to_search_coll OCCURS 0,
      name TYPE tabname,
    END OF lt_tables_to_search_coll.
lt_tables_to_search_coll-name = 'TAB_01'. 
APPEND lt_tables_to_search_coll.
lt_tables_to_search_coll-name = 'TAB_02'. 
APPEND lt_tables_to_search_coll.
lt_tables_to_search_coll-name = 'TAB_03'. 
APPEND lt_tables_to_search_coll.
lt_tables_to_search_coll-name = 'TAB_04'. 
APPEND lt_tables_to_search_coll.
lt_tables_to_search_coll-name = 'TAB_05'. 
APPEND lt_tables_to_search_coll.
lt_tables_to_search_coll-name = 'TAB_06'. 
APPEND lt_tables_to_search_coll.
lt_tables_to_search_coll-name = 'TAB_07'. 
APPEND lt_tables_to_search_coll.
lt_tables_to_search_coll-name = 'TAB_08'. 
APPEND lt_tables_to_search_coll.

So now I suppose I make a loop to go over this table. In this loop I go into the select statement and add the found values to a previously declared table.

What I tried is:

PARAMETERS: p_id    TYPE "id-type"

START-OF-SELECTION.

LOOP AT lt_tables_to_search_coll.
DATA: lv_current_table VALUE lt_tables_to_search_coll-name.

  SELECT tabname AS table_id ddtext AS table_description 
    COUNT(*) AS nr_of_records  FROM (lv_current_table)
  INTO TABLE lt_where_used_data_of_coll
  WHERE id = p_id AND ddlanguage = 'EN'
  GROUP BY tabname ddtext.

ENDLOOP.

When I run this however I get the error that lt_tables_to_search_coll-name is not a constant. I would like to know I should implement what I am trying to do.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Erik
  • 361
  • 5
  • 16
  • Why do you declare an extra variable for the dynamic select? –  Jul 11 '17 at 13:52
  • If the tables are similar enough to contain the same data, why is there 8 of them? If you just want to know where the data is, SELECT SINGLE is faster than count. – András Jul 11 '17 at 14:31
  • @András I really need to know how many times the table contains a record with a given ID. Is the speed between count and single significant enough to first check if the ID exists with SELECT SINGLE and only if it exists do the COUNT? – Erik Jul 11 '17 at 15:35
  • No, if you need the count, don't do the select single. – András Jul 11 '17 at 15:38
  • Just as a side note, avoid using `OCCURS 0` and creating tables with header lines. [This](https://stackoverflow.com/documentation/abap/1647/internal-tables/19518/declaration-of-abap-internal-tables#t=201707111540087043945) should walk you through creating a header-less table. – gkubed Jul 11 '17 at 15:41
  • I stumbled upon that information after uploading this question. I have already adapted my code, thanks! – Erik Jul 11 '17 at 15:45

2 Answers2

1

When I run this however I get the error that lt_tables_to_search_coll-name is not a constant.

The declaration DATA: lv_current_table VALUE lt_tables_to_search_coll-name. is invalid. The VALUE addition may only be used when the right-hand is a constant value, such as a number or a string.

You can make a normal declaration of lv_current_table (giving it a type) and then assign it the value of lt_tables_to_search_coll-name.

ABAP is sadly not as flexible with expressions as other languages.


I would like to know I should implement what I am trying to do.

You're better off looping at the table into a dynamic structure using DATA() to mitigate the declaration completely.

PARAMETERS: p_id    TYPE "id-type"

START-OF-SELECTION.

LOOP AT lt_tables_to_search_coll INTO DATA(ls_tables_to_search_coll).

  SELECT tabname AS table_id ddtext AS table_description 
    COUNT(*) AS nr_of_records  FROM (ls_tables_to_search_coll-name)
  INTO TABLE lt_where_used_data_of_coll
  WHERE id = p_id AND ddlanguage = 'EN'
  GROUP BY tabname ddtext.

ENDLOOP.
gkubed
  • 1,849
  • 3
  • 32
  • 45
  • 1
    Thanks for clarifying the data assignment. I understand what you are doing. After some more research and help of a colleague I did basically what you are doing but with field symbols. I will upload my solution as an answer too. But the next question that follows is, which method is faster. Using field symbols or dynamic? @gkubed – Erik Jul 11 '17 at 15:34
  • 1
    @Erik I have no solid data on hand, but on an SCN thread a while back some of us ran performance tests, and for large loads, field symbols outperformed the `DATA()` call. Unsure about small loads. – gkubed Jul 11 '17 at 15:38
  • 1
    thanks for that information. The tables in question contain hundreds of thousands of records. So I guess I should stay with the field symbols. – Erik Jul 11 '17 at 15:43
  • I'd recommend that then as well! If you get stuck, use the code at the bottom of [this example](https://stackoverflow.com/documentation/abap/4442/dynamic-programming/15522/field-symbols#t=201707111545395207572) for reference. – gkubed Jul 11 '17 at 15:46
0

Learn LOOP syntax. You shouldn't declare lv_current_table in each iteration. Use like this:

LOOP AT lt_tables_to_search_coll INTO DATA(lv_current_table).

Correspondingly your FROM will be

FROM (lv_current_table-name)
Suncatcher
  • 10,355
  • 10
  • 52
  • 90
  • 1
    Thanks for your response, it was indeed dumb of me to declare the lv_current_table in each iteration. That was not my intention. – Erik Jul 11 '17 at 15:37