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 get the PRIMARY KEY of a table at runtime ???

simadri_sekhar
Participant
0 Kudos

Hi everybody,

My requirement is to update a z-table at runtime according to the data given ata runtime. So the main difficulties for me is that whenever I tried to update the table based on the data given in UI many rows are getting updated as these fields are not unique. So I think if I can get the PRIMARY KEY along with these given fields then I can update only one row at a time.

So Can anyone suggest me how to get PRIMARY KEY of a table based on the data in UI..??

Any suggestions will be appreciated..

Thanks,

Sekhar

1 ACCEPTED SOLUTION

ravi_lanjewar
Contributor
0 Kudos

Hi,

You have to pass the table name to following function module, It provide the key and non key fields of table


* Read table fields
    CALL FUNCTION 'CACS_GET_TABLE_FIELDS'
      EXPORTING
        i_tabname     = p_table    " Table Name
      TABLES
        t_keyfield    = lt_keyfield   " Key Fields
        t_nonkeyfield = lt_nonkeyfield. " Non Key Fields

8 REPLIES 8

ravi_lanjewar
Contributor
0 Kudos

Hi,

You have to pass the table name to following function module, It provide the key and non key fields of table


* Read table fields
    CALL FUNCTION 'CACS_GET_TABLE_FIELDS'
      EXPORTING
        i_tabname     = p_table    " Table Name
      TABLES
        t_keyfield    = lt_keyfield   " Key Fields
        t_nonkeyfield = lt_nonkeyfield. " Non Key Fields

Former Member
0 Kudos

hi,

You can use this FM 'BDL_DDIF_TABL_GET' to get the key field of a particular table.

simadri_sekhar
Participant
0 Kudos

Hi Ravi and Hari,

Thanks for your valuable replies. But I want the Primary Key for those entries which are input through UI. Means the thought process is like I have to search the whole table for the entries made through UI and if fund those entries in a row fetch the primary key..

Kindly direct me a path in this regard.

Thanks,

sekhar

monika_dhumal
Participant
0 Kudos

U will get primary keys of transparent tables using FM "REUSE_FIELDCALATLOG_MERGE".

pass tyour table name and u will get fieldcatalog filled. In tht their is a field called "KEY" if it has 'X' then tht field is a primary key of transparent tables & if it has "space" then it is not a primary key...

Or else you can try with 'DDIF_TABT_GET'

Or try this code


PARAMETERS: p_table TYPE tabname OBLIGATORY.
 
DATA: go_strucdescr   TYPE REF TO cl_abap_structdescr,
      gt_tab_fields   TYPE ddfields.
 
FIELD-SYMBOLS: <gwa_tab_field> TYPE dfies.
 
TRY .
*   Get the details of the DDIC table
    go_strucdescr ?= cl_abap_elemdescr=>describe_by_name( p_table ).
  CATCH cx_sy_move_cast_error .
    MESSAGE 'Error while casting' TYPE 'S'. RETURN.
ENDTRY.
 
* Check if input is a DDIC table
CHECK go_strucdescr->is_ddic_type( ) = 'X'.
 
* Get the details of the table fields
gt_tab_fields = go_strucdescr->get_ddic_field_list( ).
 
* Display the Key fields of the table
LOOP AT gt_tab_fields ASSIGNING <gwa_tab_field> WHERE keyflag = 'X'.
  WRITE: / <gwa_tab_field>-fieldname.
ENDLOOP.

0 Kudos

hello monika,

sorry bymistakely I closed this thread.

In the selection screen what parameter I supposed to give.??

thanks in advance,

Sekhar

simadri_sekhar
Participant
0 Kudos

Thanks all...

simadri_sekhar
Participant
0 Kudos

sorry bymistakely i closed this thread...

simadri_sekhar
Participant
0 Kudos

thanks to all