Application Development 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: 

How to find out the primary key column of a database table?

Former Member
0 Kudos

Hi

Given the following scenario :

Given an inputfield, the user can enter a table name. The code behind will base on the table name given and extract out the fieldname of the primary key and concatenate the two field to become a unique string.

Eg. Order ID and Product ID make out a primary key.

How do i achieve that? Any code sample?

Regards,

Rayden

1 ACCEPTED SOLUTION

JozsefSzikszai
Active Contributor
0 Kudos

hi Rayden,

the table fields you find in table DD03L. Where the field KEYFLAG is set to X for a given field, those are key fields.

ec

13 REPLIES 13

Former Member
0 Kudos

Hi

use the table DD03L to find the field names of that table and take the first 2 fields and concatenate, you can try this

use the fun module

GET_KEY_FIELDS_OF_TABLE

Regards

Anji

Message was edited by:

Anji Reddy Vangala

0 Kudos

Hi Anji,

Thanks for the reply. Different table have different set of primary key, how do i determine how many primary key the table have and i need to extract all the primary key's data out to concat. Is it all the primary key belong to the first few keys?

Regards,

Rayden

Message was edited by:

Rayden

0 Kudos

Hi Rayden,

Combination of all key fields is the primary key, so concatenate all the key fields returned by the FM as below.

data: lv_keyfield type string.

Use FM GET_KEY_FIELDS_OF_TABLE to get the key fields.

Loop at the internal table KEY_FIELDTAB.

concatenate KEY_FIELDTAB-FIELDNAME lv_keyfield into lv_keyfield.

endloop.

This should solve your problem.

Lokesh

<b><REMOVED BY MODERATOR></b>

Message was edited by:

Lokesh Aggarwal

Message was edited by:

Alvaro Tejada Galindo

0 Kudos

Hi Lokesh,

My minisap version 4.6D don't seem to have FM GET_KEY_FIELDS_OF_TABLE. I using SE37 to access, but it state there is not sure mofule. Is there any other module to leverage on?

Regards,

Rayden

JozsefSzikszai
Active Contributor
0 Kudos

hi Rayden,

the table fields you find in table DD03L. Where the field KEYFLAG is set to X for a given field, those are key fields.

ec

0 Kudos

Hi Eric,

How do i access to DD03L using code?

Regards,

Rayden

0 Kudos

You can write a SELECT statement on DD03L to get all primary keys of a table. Pass KEYFLAG = 'X' in your conditions.

DATA: L_STRING TYPE STRING.

DATA: BEGIN OF IT_DATA OCCURS 0,

FIELDNAME TYPE DD03L-FIELDNAME,

END OF IT_DATA.

SELECT FIELDNAME

INTO TABLE IT_DATA

FROM DDD03L

WHERE TABNAME = <user entered table name> and

KEYFLAG = 'X'.

IF SY-SUBRC EQ 0.

LOOP AT IT_DATA.

CONCATENATE L_STRING IT_DATA-FIELDNAME INTO L_STRING SEPARATED BY SPACE.

ENDLOOP.

ENDIF.

ashish

Message was edited by:

Ashish Gundawar

0 Kudos

SELECT whatever

FROM DDL03

INTO whereever

WHERE tabname EQ ...

AND keyflag EQ 'X'.

0 Kudos

Hi,


select fieldname into table i_field from dd03l where tabname eq v_tabname 
                           and keyflag = 'X'.

delete i_field where fieldname eq 'MANDT'.

0 Kudos

Check the below code :

REPORT zTest no standard page heading.

data : i_DD03P like DD03P occurs 0 with header line.

data v_table like DCOBJDEF-NAME.

parameters : p_table like dd02l-tabname.

start-of-selection.

v_table = p_table.

CALL FUNCTION 'BDL_DDIF_TABL_GET'

EXPORTING

NAME = v_table

STATE = 'A'

LANGU = 'E'

  • IMPORTING

  • GOTSTATE =

  • DD02V_WA =

  • DD09L_WA =

TABLES

DD03P_TAB = i_DD03P

  • DD05M_TAB =

  • DD08V_TAB =

  • DD12V_TAB =

  • DD17V_TAB =

EXCEPTIONS

ILLEGAL_INPUT = 1

OTHERS = 2

.

IF SY-SUBRC <> 0.

  • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

  • WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

loop at i_DD03P.

if i_dd03p-KEYFLAG = 'X'.

write:/ 'Key fields', i_dd03p-FIELDNAME.

endif.

endloop.

Thanks

Seshu

Former Member
0 Kudos

Hi Raydan,

You can go to the table DD03L and give your the table name. You will find all the fields of the table and select based on the KEYFLAG = X. Then you can able to find all the key fields of the particular table and then concatenete it.

Thanks

Lincon

former_member194669
Active Contributor
0 Kudos

Hi,

Check for fm

DB_GET_ALL_INDEXES

DB_GET_ALL_TABLES

DB_GET_ALL_VIEWS

DB_GET_INDEXES

DB_GET_INDEX_FIELDS

DB_GET_INDFIELDS

Former Member
0 Kudos

You can use FM : BDL_DDIF_TABL_GET

Pass Table name ,Language

You will see output ,here if field k is X then this is primary key

Thanks

Seshu