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

Check value against foreign key

Former Member
0 Likes
1,615

Hi,

I was wondering if there is a function module to do a check against foreign keys.

To make things simple:

form check_input

USING imv_tabname " Contains the name of my table

imv_fieldname "contains the field of the table

imv_value.

In this form I want to check if the value in imv_value is valid for my field in this table. I thought about reading manually reading the check table in table DD03L, but some fields are dependant of 2 tables, so how to solve that? Anyone with some experience regarding this issue?

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,035

Hello Bjorn,

I found something in my deepest archive. That's a customer program which displays keys and indexes for all sorts of tables (TRANSP, POOL, CLUSTER). Sorry! It's back to 1997 and therefore doesn't adhere the latest ABAP syntax (e.g. OCCURS shouldn't be used anymore). But it works! You have to use the 2 additional tables DD02L and DD17S (see the code). Try it:

REPORT (SY-REPID). 
*----------------------------------------------------------------------* 
* Report:        Z_KEY_DISPLAY      formerly  ZSTLINES 
* Author:        Heinz H. Horner 
* Date:          06/11/08 
* Functionality: Displays the number of lines and primary and 
*                secondary keys for specified transparent-, 
*                pool- and cluster tables. 
* Transaction:   ?? 
**----------------------------------------------------------------------* 
TABLES: DD02L, DD03L, DD17S. 

PARAMETERS: IN_TABLE like dd02l-tabname. 

DATA: BEGIN OF TAB_KEY OCCURS 5. 
        INCLUDE STRUCTURE DD03L. 
DATA: END OF TAB_KEY. 

DATA: BEGIN OF TAB_SEC OCCURS 5. 
        INCLUDE STRUCTURE DD17S. 
DATA: END OF TAB_SEC. 

* run time fields 
DATA: TIME_B    TYPE P, 
      TIME_A    TYPE P, 
      TIME_DIFF LIKE SY-DBCNT. 

* working fields 
DATA: COUNT LIKE SY-DBCNT. 


START-OF-SELECTION. 
  SELECT * FROM DD02L 
           WHERE TABNAME = IN_TABLE. 
    EXIT. 
  ENDSELECT. 

  IF SY-SUBRC <> 0. 
    WRITE: / IN_TABLE, 'is not defined in the System table'. 
    EXIT. 
  ELSE. 
    IF DD02L-TABCLASS <> 'TRANSP'  AND 
       DD02L-TABCLASS <> 'POOL'    AND 
       DD02L-TABCLASS <> 'CLUSTER'. 
      WRITE: / IN_TABLE, 'is not a transparent/pool/cluster table'. 
      EXIT. 
    ENDIF. 
  ENDIF. 

* select key-fields 
  SELECT * FROM DD03L INTO TABLE TAB_KEY 
           WHERE TABNAME = IN_TABLE AND 
                 KEYFLAG = 'X'. 
  SORT TAB_KEY BY POSITION. 

* select secondary key-fields only for transparent tables 
  IF DD02L-TABCLASS = 'TRANSP'. 
    SELECT * FROM DD17S INTO TABLE TAB_SEC 
             WHERE SQLTAB = IN_TABLE. 
  ENDIF. 

* Direct selection via variable tablename. 
  GET RUN TIME FIELD TIME_B.           "time before counting 

  SELECT COUNT(*) FROM (IN_TABLE). 
  IF SY-SUBRC <> 0. 
    WRITE: / 'Counting Error.'. 
    WRITE:   'Please try it later again.'. 
    EXIT. 
  ENDIF. 

  GET RUN TIME FIELD TIME_A.           "time after counting 
  TIME_DIFF = TIME_A - TIME_B.         "time difference in microseconds 

  COUNT = SY-DBCNT. 
  WRITE: / 'The', DD02L-TABCLASS, 'table', IN_TABLE, 
           'contains', COUNT, 'records.'. 
  WRITE: / 'Select-Time:', TIME_DIFF UNDER COUNT, 'microseconds.', /. 

* output of the key-fields. 
  WRITE: / 'Primary Keys:'. 
  LOOP AT TAB_KEY. 
    IF SY-TABIX <> 1. 
      WRITE: / ' '. 
    ENDIF. 
    WRITE: 17 TAB_KEY-FIELDNAME. 
  ENDLOOP. 

  IF DD02L-TABCLASS <> 'TRANSP'. 
    WRITE: /, 'No secondary keys for tableclass', DD02L-TABCLASS. 
  ELSE. 
    WRITE: /, 'Secondary Keys:', 28 'Index:'. 
    LOOP AT TAB_SEC. 
      IF SY-TABIX <> 1. 
        WRITE: / ' '. 
      ENDIF. 
      WRITE: 17 TAB_SEC-FIELDNAME, 35 TAB_SEC-INDEXNAME. 
    ENDLOOP. 
  ENDIF.

Have success,

Heinz

Hi,

I was wondering if there is a function module to do a check against foreign keys.

To make things simple:

form check_input

USING imv_tabname " Contains the name of my table

imv_fieldname "contains the field of the table

imv_value.

In this form I want to check if the value in imv_value is valid for my field in this table. I thought about reading manually reading the check table in table DD03L, but some fields are dependant of 2 tables, so how to solve that? Anyone with some experience regarding this issue?

3 REPLIES 3
Read only

Former Member
0 Likes
1,036

Hello Bjorn,

I found something in my deepest archive. That's a customer program which displays keys and indexes for all sorts of tables (TRANSP, POOL, CLUSTER). Sorry! It's back to 1997 and therefore doesn't adhere the latest ABAP syntax (e.g. OCCURS shouldn't be used anymore). But it works! You have to use the 2 additional tables DD02L and DD17S (see the code). Try it:

REPORT (SY-REPID). 
*----------------------------------------------------------------------* 
* Report:        Z_KEY_DISPLAY      formerly  ZSTLINES 
* Author:        Heinz H. Horner 
* Date:          06/11/08 
* Functionality: Displays the number of lines and primary and 
*                secondary keys for specified transparent-, 
*                pool- and cluster tables. 
* Transaction:   ?? 
**----------------------------------------------------------------------* 
TABLES: DD02L, DD03L, DD17S. 

PARAMETERS: IN_TABLE like dd02l-tabname. 

DATA: BEGIN OF TAB_KEY OCCURS 5. 
        INCLUDE STRUCTURE DD03L. 
DATA: END OF TAB_KEY. 

DATA: BEGIN OF TAB_SEC OCCURS 5. 
        INCLUDE STRUCTURE DD17S. 
DATA: END OF TAB_SEC. 

* run time fields 
DATA: TIME_B    TYPE P, 
      TIME_A    TYPE P, 
      TIME_DIFF LIKE SY-DBCNT. 

* working fields 
DATA: COUNT LIKE SY-DBCNT. 


START-OF-SELECTION. 
  SELECT * FROM DD02L 
           WHERE TABNAME = IN_TABLE. 
    EXIT. 
  ENDSELECT. 

  IF SY-SUBRC <> 0. 
    WRITE: / IN_TABLE, 'is not defined in the System table'. 
    EXIT. 
  ELSE. 
    IF DD02L-TABCLASS <> 'TRANSP'  AND 
       DD02L-TABCLASS <> 'POOL'    AND 
       DD02L-TABCLASS <> 'CLUSTER'. 
      WRITE: / IN_TABLE, 'is not a transparent/pool/cluster table'. 
      EXIT. 
    ENDIF. 
  ENDIF. 

* select key-fields 
  SELECT * FROM DD03L INTO TABLE TAB_KEY 
           WHERE TABNAME = IN_TABLE AND 
                 KEYFLAG = 'X'. 
  SORT TAB_KEY BY POSITION. 

* select secondary key-fields only for transparent tables 
  IF DD02L-TABCLASS = 'TRANSP'. 
    SELECT * FROM DD17S INTO TABLE TAB_SEC 
             WHERE SQLTAB = IN_TABLE. 
  ENDIF. 

* Direct selection via variable tablename. 
  GET RUN TIME FIELD TIME_B.           "time before counting 

  SELECT COUNT(*) FROM (IN_TABLE). 
  IF SY-SUBRC <> 0. 
    WRITE: / 'Counting Error.'. 
    WRITE:   'Please try it later again.'. 
    EXIT. 
  ENDIF. 

  GET RUN TIME FIELD TIME_A.           "time after counting 
  TIME_DIFF = TIME_A - TIME_B.         "time difference in microseconds 

  COUNT = SY-DBCNT. 
  WRITE: / 'The', DD02L-TABCLASS, 'table', IN_TABLE, 
           'contains', COUNT, 'records.'. 
  WRITE: / 'Select-Time:', TIME_DIFF UNDER COUNT, 'microseconds.', /. 

* output of the key-fields. 
  WRITE: / 'Primary Keys:'. 
  LOOP AT TAB_KEY. 
    IF SY-TABIX <> 1. 
      WRITE: / ' '. 
    ENDIF. 
    WRITE: 17 TAB_KEY-FIELDNAME. 
  ENDLOOP. 

  IF DD02L-TABCLASS <> 'TRANSP'. 
    WRITE: /, 'No secondary keys for tableclass', DD02L-TABCLASS. 
  ELSE. 
    WRITE: /, 'Secondary Keys:', 28 'Index:'. 
    LOOP AT TAB_SEC. 
      IF SY-TABIX <> 1. 
        WRITE: / ' '. 
      ENDIF. 
      WRITE: 17 TAB_SEC-FIELDNAME, 35 TAB_SEC-INDEXNAME. 
    ENDLOOP. 
  ENDIF.

Have success,

Heinz

Read only

Former Member
0 Likes
1,035

Hi Heinz,

thanks for your answer, but I found this function module, which does the same with a lot less effort for me

'SD_SCD_FOREIGN_KEY_CHECK'

Read only

0 Likes
1,035

Thank you Bjorn,

I will place your found FM in my memory and in my archive.

Regards, Heinz