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

Internal Table or equivalent having primary key

Former Member
7,304

Hello,

I want to use a table in my abap code. First I tried to do it like that:

DATA: Begin of adress occurs 3,
        vorname(25) type c,
	 nachname(25) type c,
	 wohnort(25) type c,
end of adress.
 
*-----Insert Dataset
adress-vorname = 'Test'.
adress-nachname = 'Test'.
adress-wohnort = 'Test'.
append adress.
 
*-----Insert another Dataset
adress-vorname = 'Test2'.
adress-nachname = 'Test2'.
adress-wohnort = 'Test2'.
append adress.

Then someone in this forum told that like that I can't use primary keys. So I thought I could do it like that:

TYPES: BEGIN OF LINE,
COLUMN1 TYPE c,
COLUMN2 TYPE c,
COLUMN3 TYPE c,
END OF LINE.

TYPES ITAB TYPE SORTED TABLE OF LINE WITH UNIQUE KEY COLUMN1.

DATA: tabellePrim type LINE.

*-----Get Values for first Dataset
tabellePrim-COLUMN1 = 'Aaa'.
tabellePrim-COLUMN2 = 'cfd'.
tabellePrim-COLUMN3 = 'eee'.
append tabellePrim.

But the writing of my dataset doesn't work. Can you tell me where the mistake is? Or can you tell me the best solution for the problem (having table with primary key)?

1 ACCEPTED SOLUTION
Read only

Former Member
2,651

Hi

Do it like in this way.

data: BEGIN OF line1,
column1 TYPE c,
column2 TYPE c,
column3 TYPE c,
END OF line1.

data: itab like sorted TABLE OF line1 WITH UNIQUE KEY column1.

DATA: tabelleprim TYPE line.

*-----Get Values for first Dataset
line1-column1 = 'Aaa'.
line1-column2 = 'cfd'.
line1-column3 = 'eee'.
APPEND line1 TO itab.

Regards

Anup.

Hi

Do it like in this way.

data: BEGIN OF line1,
column1 TYPE c,
column2 TYPE c,
column3 TYPE c,
END OF line1.

data: itab like sorted TABLE OF line1 WITH UNIQUE KEY column1.

DATA: tabelleprim TYPE line.

*-----Get Values for first Dataset
line1-column1 = 'Aaa'.
line1-column2 = 'cfd'.
line1-column3 = 'eee'.
APPEND line1 TO itab.

Regards

Anup.

10 REPLIES 10
Read only

RaymondGiuseppi
Active Contributor
0 Likes
2,651

With a SORTED TABLE you have to use INSERT or COLLECT not APPEND which can abort if you don't respect the sort order (or create duplicate key if unique key specified)

Look at [Filling Internal Tables Line By Line|http://help.sap.com/erp2005_ehp_03/helpdata/EN/fc/eb36e2358411d1829f0000e829fbfe/frameset.htm]

Regards

Read only

Sm1tje
Active Contributor
0 Likes
2,651

Create a type:

types: begin of ty_address,

vorname(25) type c,

nachname(25) type c,

wohnort(25) type c,

types: end of ty_address.

data: gt_address type table of ty_address with unique key vorname.

data: wa_address type ty_address.

wa_address-vorname = 'AAA'.

wa_address-nachname = 'BBB'.

wa_address-wohnort = 'CCC'.

insert wa_address into table gt_address.

Beware:

1. If you define UNIQUE key, than make sure that this field is UNIQUE. In this case, vorname is defined as unique, but there will probably be more persons with same VORNAME. But this is just an example.

2. When Unique key, you will have to use insert (in stead of APPEND) to make sure key specification is not violated, you will get a short dump.

Read only

Former Member
0 Likes
2,651

Hello Mickey,

your code has some errors.

I think there is a redundant Types:

As well there is the syntax error saying: unique only can be used in hased or sorted tables.

What do I need to change to create a sorted table?

Read only

Former Member
2,652

Hi

Do it like in this way.

data: BEGIN OF line1,
column1 TYPE c,
column2 TYPE c,
column3 TYPE c,
END OF line1.

data: itab like sorted TABLE OF line1 WITH UNIQUE KEY column1.

DATA: tabelleprim TYPE line.

*-----Get Values for first Dataset
line1-column1 = 'Aaa'.
line1-column2 = 'cfd'.
line1-column3 = 'eee'.
APPEND line1 TO itab.

Regards

Anup.

Read only

0 Likes
2,651

Hi anup,

your code works so far. But how can I access the datasets in the table:

loop at adress.
write : / line1-vorname, line1-nachname, line1-wohnort.
endloop.

doesn't work anymore

Read only

0 Likes
2,651

Daniel,

sorry for that, NO sap system at hand right now. But indeed use TYPE SORTED TABLE OF like Anup suggested.

loop at adress into wa_address.

write : / wa_address-vorname, wa_address-nachname, wa_address-wohnort.

endloop.

Read only

0 Likes
2,651

Hi,

my code doesn't work yet. I found this code on abap examples:

DATA: ftab2 TYPE SORTED TABLE OF f
            WITH NON-UNIQUE KEY table_line,
      itab1 TYPE HASHED TABLE OF i
            WITH UNIQUE KEY table_line,
      fl    TYPE f.

DO 3 TIMES.
  INSERT sy-index INTO TABLE itab1.
ENDDO.

ftab2 = itab1.

LOOP AT ftab2 INTO fl.
  WRITE: / fl.
ENDLOOP.

SKIP.
ULINE.

I tried to change it so it hits my requirements: The Part thats interesting is itab1. So my idea was to change "itab1 TYPE HASHED TABLE OF i" to " itab1 TYPE HASHED TABLE OF c". I want to work with Alphanumm values.

So I wanted to try :

DO 3 TIMES.
  INSERT 'a' INTO TABLE itab1.
ENDDO.
LOOP.
  WRITE: / itab1.
ENDLOOP.

I get an error at "WRITE: / itab1." saying "ITAB1 can't be converted to char field"

Read only

0 Likes
2,651
data: BEGIN OF line1,
column1(3) TYPE c,
column2(3) TYPE c,
column3(3) TYPE c,
END OF line1.

data: itab like sorted TABLE OF line1 WITH UNIQUE KEY column1.

DATA: tabelleprim TYPE line.

*if internal table is of type sorted table
*then Populate data in sort order otherwise it will throw an exception.

*-----Get Values for first Dataset
line1-column1 = 'Aaa'.
line1-column2 = 'cfd'.
line1-column3 = 'eee'.
APPEND line1 TO itab.

line1-column1 = 'Aab'.
line1-column2 = 'cfd'.
line1-column3 = 'eee'.
APPEND line1 TO itab.

line1-column1 = 'Aac'.
line1-column2 = 'cfd'.
line1-column3 = 'eee'.
APPEND line1 TO itab.


line1-column1 = 'Aad'.
line1-column2 = 'cfd'.
line1-column3 = 'eee'.
APPEND line1 TO itab.

loop at itab into line1.

write:/
   line1-column1,
  line1-column2,
  line1-column3.
endloop.
Read only

Former Member
0 Likes
2,651

Hi,

try like this,

read table itab into WA with key condition.

wa-vorname = 'Test'.

wa-nachname = 'Test'.

wa-wohnort = 'Test'.

modify itab with wa transporting vorname,nachname,wohnort.

pls reward if useful.

Read only

Former Member
0 Likes
2,651

Hi,

There are some syntax error saying: unique only can be used in hased or sorted tables.check the code once.

Regards,

Muneesh Gitta.