1

I need a SELECT statement in ABAP, but the problem is, that the table does not exist on that system.

I am checking the existence of the table with the FM:

CALL FUNCTION 'DDIF_TABL_GET'
  EXPORTING
     name     = 'mytable'
  IMPORTING
     gotstate = l_got_state
  EXCEPTIONS
     OTHERS   = 1.

IF sy-subrc = 0.
  SELECT SINGLE * FROM mytable  INTO  mylocalstructure WHERE ...........  .
ENDIF.

But there is still a syntax error:

"mytable" is not defined in the ABAP Dictionary as a table

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
user3405132
  • 11
  • 1
  • 2

5 Answers5

9

There is a pure ABAP way to check if a table exists at runtime without using a function module. The SELECT statement allows to pass the name of the table as a clike variable or string literal by putting it in parentheses. In that case the table name will be checked at runtime, not at compile-time. When it doesn't exist, an exception of type CX_SY_DYNAMIC_OSQL_SEMANTICS is thrown, which you can catch:

TRY.
    SELECT * FROM ('mytable') INTO mylocalstructure WHERE ........... 
  CATCH CX_SY_DYNAMIC_OSQL_SEMANTICS.
    MESSAGE 'Table does not exist' TYPE 'S' DISPLAY LIKE 'E'.
ENDTRY.
Philipp
  • 67,764
  • 9
  • 118
  • 153
1

did you write

  EXPORTING
     name     = 'mytable'

or

  EXPORTING
     name     = 'MYTABLE'

In ABAP it often is important to write in uppercaseif you use ' '.

Jay
  • 113
  • 12
  • That would just cause the function module to not work. It would not cause a syntax error when activating the program. – Philipp Mar 11 '14 at 12:41
  • Are you sure? I´ve got a syntax-error because of this. After changing to uppercase the programm works fine. – Jay Mar 11 '14 at 13:11
1

You can also try to select from table DD03L.

DATA lv_mytabname TYPE tablename.

lv_mytabname = 'ZTABLE'.

SELECT SINGLE FIELDNAME

FROM DD03L

INTO lv_mytabname

WHERE FIELDNAME EQ lv_mytabname.

IF sy-subrc EQ 0.

* TABLE EXISTS!

ENDIF.
KAA
  • 11
  • 2
0

Just go to transaction SE11 and check for the existence of the table. This is pretty obvious to ABAP developers. No need for function modules.

If the table does not exist on your system, you cannot select from it. Also obvious. Some tables only exist on some systems; ie. most ERP tables will only exist on ERP boxes (you won't find MARA in an HR box, for instance).

Regards, Trond

0

If what you want its to byPass the error, you can assign the name of the table to a field symbol.

But if what you want its just to know why are you getting that error, its because the lowercase in your hardcode definition.

And if it still get that error, the Occam knive tell us to check if the table really exists in the ABAP Dictionary (se11)

Isidorito
  • 11
  • 1
  • 6