5

How to add constraint for a field in a table that should only contain unique values in ABAP?

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Sathya
  • 69
  • 1
  • 2

3 Answers3

7

So, in your table; you wish there weren't any NON-KEY attributes, right?

  • SE11 > Goto > Indexes
  • Create an INDEX on this field & make it unique
  • SY-SUBRC to be made non-zero + Use MANDT
closebrace
  • 333
  • 1
  • 4
  • 3
    Be careful though and DOCUMENT this ( put in table description even!). In my first year of ABAP I almost drove a senior developer crazy because he could not insert a record ( the debugger gives no indication as to why the record will not be inserted ). – tomdemuyt Jul 19 '11 at 18:29
1

If you want to do it using code...

ALTER TABLE dbo.MyTable ADD CONSTRAINT MyTable_Code_Name_UniqueKey UNIQUE (Code, Name)
AndrewD
  • 1,083
  • 9
  • 15
0

Setting the the field as a table key ?

as far as i know, there is no "Unique" flag for table field. If you really need such a check, you could code a control into the event (ie sm30 / Environnement / Modify / Events) before the data is writed to the DB.

regards

PATRY Guillaume
  • 4,287
  • 1
  • 32
  • 41
  • I have set 3 fields as table keys, combinations. so the particular field has to be unique.... – Sathya May 17 '11 at 09:26
  • 2
    Why do you have 3 fields as table keys if one of them by itself will be unique? Couldn't you leave that one field as the key make indexes (if you need them) on the other 2? – Bryan Cain May 17 '11 at 13:43