简体   繁体   English

检查表是否存在

[英]Check if the table exists

I need a SELECT statement in ABAP, but the problem is, that the table does not exist on that system.我需要在 ABAP 中使用SELECT语句,但问题是该表在该系统上不存在。

I am checking the existence of the table with the FM:我正在用 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 “mytable”未在 ABAP 词典中定义为表

There is a pure ABAP way to check if a table exists at runtime without using a function module.有一种纯 ABAP 方法可以在运行时检查表是否存在,而无需使用功能模块。 The SELECT statement allows to pass the name of the table as a clike variable or string literal by putting it in parentheses. SELECT语句允许将表的名称作为 clike 变量或字符串文字通过将其放在括号中来传递。 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:如果它不存在,则会抛出CX_SY_DYNAMIC_OSQL_SEMANTICS类型的异常,您可以捕获该异常:

TRY.
    SELECT * FROM ('mytable') INTO mylocalstructure WHERE ........... 
  CATCH CX_SY_DYNAMIC_OSQL_SEMANTICS.
    MESSAGE 'Table does not exist' TYPE 'S' DISPLAY LIKE 'E'.
ENDTRY.

did you write你写了吗

  EXPORTING
     name     = 'mytable'

or或者

  EXPORTING
     name     = 'MYTABLE'

In ABAP it often is important to write in uppercaseif you use ' '.在 ABAP 中,如果使用“ ”,则大写通常很重要。

You can also try to select from table DD03L.您也可以尝试从表 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.

Just go to transaction SE11 and check for the existence of the table.只需转到事务 SE11 并检查该表是否存在。 This is pretty obvious to ABAP developers.这对 ABAP 开发人员来说非常明显。 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. IE。 most ERP tables will only exist on ERP boxes (you won't find MARA in an HR box, for instance).大多数 ERP 表仅存在于 ERP 盒中(例如,您不会在 HR 盒中找到 MARA)。

Regards, Trond问候, 特隆

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)如果它仍然得到那个错误,奥卡姆刀告诉我们检查该表是否真的存在于 ABAP 词典 (se11)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM