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

ABAP Code not working

adeel_sarwar
Contributor
0 Likes
4,312

Hi,

I have made a query and in the query I have created an additional field to find any duplicates in a table. When I check the code there is no error which says to me the code is ok however when I run the query it brings a result of 0 even though there are duplicate production order numbers. What am I doing wrong?

Please see below code if you can change this and let me know i would appreciate this.

Points will be awarded thanks

DATA : BEGIN OF itab OCCURS 0,

aufnr LIKE ekkn-aufnr,

END OF itab.

DATA : count(2) TYPE p.

DATA : no_of_records(3) TYPE p.

Clear no_of_records.

select aufnr from ekkn into itab.

endselect.

SORT itab BY aufnr.

LOOP AT itab.

ADD 1 TO count.

AT END OF aufnr.

IF count GE 2.

ADD count TO no_of_records.

ENDIF.

CLEAR count.

ENDAT.

ENDLOOP.

DUPLICATES = NO_of_records.

1 ACCEPTED SOLUTION
Read only

kesavadas_thekkillath
Active Contributor
0 Likes
4,028

DATA : BEGIN OF itab OCCURS 0,

aufnr LIKE ekkn-aufnr,

END OF itab.

data:BEGIN OF itab_final OCCURS 0,

aufnr LIKE ekkn-aufnr,

count type p decimals 0,

end of it_final.

data dup_cnt type p decimals 0.

data:wk_total type p decimasl 0.

sort itab by aufnr ascending.

loop at itab.

at first.

clear dup_cnt.

clear wk_total.

endat.

at new aufnr.

clear itab_final.

itab_final-aufnr = itab-aufnr.

clear dup_cnt.

endat.

dup_cnt = dup_cnt + 1.

at end of aufnr.

if dup_cnt <= 1.

dup_cnt = dup_cnt - 1.

endif.

itab_final-count = dup_cnt.

append itab_final.

endat.

endloop.

Now itab_final holds the data...just print it...for sum of duplicate records

if u r using a normal report

at last.

sum itab_final-count.

write itab_final-count.

endat.

hope this solves all ur issues or else if u donot want to move it other itab just use my last post

i guess the majority of replies here are correct,,,,do try to debug yourself and find a solution bcoz 99 % of solution is posted by few experts here.

49 REPLIES 49
Read only

Former Member
0 Likes
1,130

hi,

i think u missed the append statement between select and endselect statements

Read only

ThomasZloch
Active Contributor
0 Likes
1,130

try:

select aufnr from ekkn into TABLE itab.

endselect.

Read only

Former Member
0 Likes
1,130

select aufnr from ekkn into ( itab-aufnr ).

append itab.

endselect.

Read only

Former Member
0 Likes
1,130

I am not a big fan of the ABAP AT end of ..... ENDAT

type of statements.

ABAP IMO doesn't always seem to handle these statements predictably.

You might find the ON CHANGE .... ENDON type of statement works better

Cheers

jimbo

Read only

i048168
Product and Topic Expert
Product and Topic Expert
0 Likes
1,130

Hi,

In your code the internal table itab is not getting populated.

use the following select statement:

select aufnr from ekkn into itab.

Then the code is working. Just check it out.

Regards

Vadi

Read only

Former Member
0 Likes
1,130

Hi try this code changes.

DATA : BEGIN OF itab OCCURS 0,

aufnr LIKE ekkn-aufnr,

END OF itab.

DATA : count TYPE i.

DATA : no_of_records TYPE i.

CLEAR no_of_records.

SELECT aufnr FROM ekkn INTO table itab.

SORT itab BY aufnr.

LOOP AT itab.

ADD 1 TO count.

AT END OF aufnr.

IF count GE 2.

ADD 1 TO no_of_records.

CLEAR count.

ENDIF.

ENDAT.

ENDLOOP.

WRITE: 'Total Duplicated Records:', no_of_records.

awrd points if useful

Bhupal

Read only

0 Likes
1,130

Hi Bhupal

Thanks for your code, i have tried it but it brings a value of 350 as duplicates for all lines. Any idea why?

thanks

Read only

0 Likes
1,130

Friend,

try this....

DATA : BEGIN OF itab OCCURS 0,

aufnr LIKE ekkn-aufnr,

cnt type i,

END OF itab.

DATA : count TYPE i.

DATA : no_of_records TYPE i.

CLEAR no_of_records.

SELECT aufnr COUNT(*) FROM ekkn INTO table itab group by aufnr.

SORT itab BY aufnr.

LOOP AT itab.

IF itab-cnt eq 2.

ADD 1 TO no_of_records.

ENDIF.

ENDLOOP.

WRITE: 'Total Duplicated Records:', no_of_records.

Edited by: Ramu on Jan 4, 2008 10:07 AM

Read only

Former Member
0 Likes
1,130

Code is looking OK....check your itab before loop.

DATA : BEGIN OF itab OCCURS 0,

aufnr LIKE ekkn-aufnr,

END OF itab.

DATA : count(2) TYPE p.

DATA : no_of_records(3) TYPE p.

CLEAR no_of_records.

*SELECT aufnr FROM ekkn INTO TABLE itab.

itab-aufnr = '33333'.

APPEND itab.

itab-aufnr = '44444'.

APPEND itab.

itab-aufnr = '44444'.

APPEND itab.

itab-aufnr = '33333'.

APPEND itab.

itab-aufnr = '33456'.

APPEND itab.

itab-aufnr = '33456'.

APPEND itab.

itab-aufnr = '33377'.

APPEND itab.

itab-aufnr = '555444'.

APPEND itab.

SORT itab BY aufnr.

LOOP AT itab.

WRITE 😕 itab-aufnr.

ADD 1 TO count.

AT END OF aufnr.

IF count GE 2.

ADD count TO no_of_records.

ENDIF.

CLEAR count.

ENDAT.

ENDLOOP.

WRITE 😕 no_of_records.

Read only

Former Member
0 Likes
1,130

hi, try this change.

DATA : BEGIN OF itab OCCURS 0,

aufnr LIKE ekkn-aufnr,

END OF itab.

DATA : count TYPE i.

DATA : no_of_records TYPE i.

CLEAR no_of_records.

SELECT aufnr FROM ekkn INTO table itab.

SORT itab BY aufnr.

LOOP AT itab.

ADD 1 TO count.

AT END OF aufnr.

IF count GE 2.

WRITE:/ 'Record', itab-aufnr, 'Duplicates:', count.

ENDIF.

CLEAR count.

ENDAT.

ENDLOOP.

awrd points if useful

Bhupal

Read only

Former Member
0 Likes
1,130

Hi Adeel

This should work.

DATA : BEGIN OF itab OCCURS 0,

aufnr LIKE ekkn-aufnr,

END OF itab.

DATA : count TYPE i.

SELECT aufnr FROM ekkn INTO TABLE itab.

SORT itab BY aufnr DESCENDING.

LOOP AT itab.

IF NOT itab-aufnr IS INITIAL.

ADD 1 TO count.

AT END OF aufnr.

IF count GE 2.

WRITE:/'Production order',itab-aufnr,'Total Duplicated' &

'Records:', count.

CLEAR count.

ENDIF.

ENDAT.

ENDIF.

ENDLOOP

Cheers

shivika

Read only

0 Likes
1,130

Hi Guys, nearlly there:)

The below code works however just a small problem as this is a query i want to pull this information through to a field called Duplicates but it brings a value of 2 everytime don't know why, but when I exit the query it shows a screen with a list of duplicates due to the write statement and the duplicates in that page are correct,

How do I pass the correct value through to the field duplicates?

DATA : BEGIN OF itab OCCURS 0,

aufnr LIKE ekkn-aufnr,

END OF itab.

DATA : count TYPE i.

SELECT aufnr FROM ekkn INTO TABLE itab.

SORT itab BY aufnr DESCENDING.

Clear Duplicates. <----


New Line Added

LOOP AT itab.

IF NOT itab-aufnr IS INITIAL.

ADD 1 TO count.

AT END OF aufnr.

IF count GE 2.

WRITE:/'Production order',itab-aufnr,'Total Duplicated' &

'Records:', count.

Duplicates = Count. <----


New Line Added

CLEAR count.

ENDIF.

ENDAT.

ENDIF.

ENDLOOP

Read only

0 Likes
1,130

Hi Adeel,

This code will work better ....try this...

DATA : BEGIN OF itab OCCURS 0,

aufnr LIKE ekkn-aufnr,

cnt type i,

END OF itab.

DATA : count TYPE i.

DATA : no_of_records TYPE i.

CLEAR no_of_records.

SELECT aufnr COUNT(*) FROM ekkn INTO table itab group by aufnr.

SORT itab BY aufnr.

write:/ 'Duplicate Records'.

LOOP AT itab.

IF itab-cnt eq 2.

ADD 1 TO no_of_records.

write:/ itab-aufnr.

ENDIF.

ENDLOOP.

WRITE: 'Total Duplicated Records:', no_of_records.

Read only

0 Likes
1,130

Hi Adeel

I guess what keshu has suggested should give you the number of duplicate records. rite?

Cheers

shivika

Read only

0 Likes
1,130

Hi Nope doesn't work atall that code

DATA : BEGIN OF itab OCCURS 0,

aufnr LIKE ekkn-aufnr,

END OF itab.

DATA : count TYPE i.

data:wk_lines type i,wk_lines2 type i,wk_dup type i.

sort itab ascending.

describe table itab lines wk_lines.

delete adjacent duplicates from itab comparing aufnr.

describe table itab lines wk_lines2.

wk_dup = wk_lines - wk_lines2.

DUPLICATES = wk_dup.

Read only

0 Likes
1,130

Friend Shivika,

Yes....Keshu coding is too good...but it will not give the duplicate aufnr list in the screen...just it will give no.of duplicates...

Read only

0 Likes
1,130

Hi Adeel

Do you want the order numbers which have duplicates on your screen output?

I could not understand your requirement for this one.

Read only

0 Likes
1,130

yep but what happens is that i have a query which lists all the Production orders in the table and line by line i want it to say if there is more than 1 record in the table or not?

All the codes are excellent but the don't do that all they do is give a list of duplicates i.e. this code

DATA : BEGIN OF itab OCCURS 0,

aufnr LIKE ekkn-aufnr,

cnt type i,

END OF itab.

DATA : count TYPE i.

DATA : no_of_records TYPE i.

CLEAR no_of_records.

SELECT aufnr COUNT(*) FROM ekkn INTO table itab group by aufnr.

SORT itab BY aufnr.

write:/ 'Duplicate Records'.

LOOP AT itab.

IF itab-cnt eq 2.

ADD 1 TO no_of_records.

write:/ itab-aufnr.

ENDIF.

ENDLOOP.

WRITE: 'Total Duplicated Records:', no_of_records.

Read only

0 Likes
1,130

What actually do u want??

Total Duplicates for each record or Total Duplicates that are fetched by the query??

Read only

0 Likes
1,130

Hi Shivika

example below

Prod Order Duplicate

1000010 3

1000020 0

1000030 0

1000040 0

1000050 0

1000010 3

1000010 3

Read only

0 Likes
1,130

Total duplicates for each record

Read only

0 Likes
1,130

I think u want numbers like item numbers na.

i.e. for each group of records we have to assign sequential numbers. Is this ur requirement??

be clear.

Read only

0 Likes
1,130

DATA : BEGIN OF itab OCCURS 0,

aufnr LIKE ekkn-aufnr,

END OF itab.

DATA : count TYPE i.

DATA : no_of_records TYPE i.

CLEAR no_of_records.

SELECT aufnr FROM ekkn INTO table itab.

SORT itab BY aufnr.

LOOP AT itab.

ADD 1 TO count.

AT END OF aufnr.

count = count - 1.

WRITE:/ 'Record', itab-aufnr, 'Duplicates:', count .

CLEAR count.

ENDAT.

ENDLOOP.

Try this it would solve ur problem

awrd points if useful

Bhupal

Read only

0 Likes
1,130

sort itab by aufnr ascending.

data:wk_aufnr type afku-aufnr.

loop at itab.

at new aufnr.

wk_aufnr = itab-aufnr.

write:/ aufnr.

clear dup_cnt.

endat.

dup_cnt = dup_cnt + 1.

at end of aufnr .

if dup_cnt > 1.

write : dup_cnt .

else.

write : '0'.

endat.

endloop.

Read only

0 Likes
1,130

Hi Adeel,

This will definetely work....

DATA : BEGIN OF itab OCCURS 0,

aufnr LIKE ekkn-aufnr,

cnt type i,

END OF itab.

DATA : count TYPE i.

DATA : no_of_records TYPE i.

DATA : BEGIN OF itab1 OCCURS 0,

aufnr LIKE ekkn-aufnr,

END OF itab1.

SELECT aufnr FROM ekkn INTO table itab1.

CLEAR no_of_records.

SELECT aufnr COUNT(*) FROM ekkn INTO table itab group by aufnr.

SORT itab BY aufnr.

write:/ 'Duplicate Records'.

LOOP AT itab1.

read table itab with key aufnr = itab1-aufnr.

write:/ itab1-aufnr, itab-cnt.

ENDLOOP.

WRITE: 'Total Duplicated Records:', no_of_records.

Edited by: Ramu on Jan 4, 2008 11:04 AM

Edited by: Ramu on Jan 4, 2008 11:08 AM

Read only

0 Likes
1,130

Hey

this should work then..

DATA : BEGIN OF itab OCCURS 0,

aufnr LIKE ekkn-aufnr,

END OF itab.

DATA : count TYPE i.

SELECT aufnr FROM ekkn INTO TABLE itab.

SORT itab BY aufnr .

LOOP AT itab.

IF NOT itab-aufnr IS INITIAL.

ADD 1 TO count.

AT END OF aufnr.

IF count GE 2.

WRITE:/'Production order',itab-aufnr,'Total Duplicated' &

'Records:', count.

CLEAR count.

else.

clear count.

WRITE:/'Production order',itab-aufnr,'Total Duplicated' &

'Records:', count.

ENDIF.

ENDAT.

ENDIF.

ENDLOOP.

Is this what you want?

Cheers

shivika

Read only

0 Likes
1,130

Hi,

yes example below for what I want the result to be when i run the query.

You can see there is a duplicate for order numner 1031374 so the result will the be the total count of duplicates.

Purch.Doc. Item Order Count/Duplicate

4500001258 00010 1031372 1

4500001259 00010 1031373 1

4500001260 00010 1031374 2

4500001261 00010 1031375 1

4500001262 00010 1031376 1

4500001263 00010 1031377 1

4500001264 00010 1031378 1

4500001265 00010 1031374 2

Read only

0 Likes
1,130

Hi Adeel,

This will definetely work....

DATA : BEGIN OF itab OCCURS 0,

aufnr LIKE ekkn-aufnr,

cnt type i,

END OF itab.

DATA : count TYPE i.

DATA : no_of_records TYPE i.

DATA : BEGIN OF itab1 OCCURS 0,

aufnr LIKE ekkn-aufnr,

END OF itab1.

SELECT aufnr FROM ekkn INTO table itab1.

CLEAR no_of_records.

SELECT aufnr COUNT(*) FROM ekkn INTO table itab group by aufnr.

SORT itab BY aufnr.

write:/ 'Duplicate Records'.

LOOP AT itab1.

read table itab with key aufnr = itab1-aufnr.

write:/ itab1-aufnr, itab-cnt.

ENDLOOP.

Read only

0 Likes
1,130

Hi Shivika,

So far your code is the nearest to what i want to achieve. but what your code does is that it writes a list of all the production orders and says if they are duplicates or not but i have a field already in the query called duplicates which I wish to pass a single value through. so basically when I run the query it already has a list of POs, Production order numbers etc so I have created a field called duplicates in which I want the value to go through that field rather than a write statement for all the production orders does that make sense? as this is a QUERY using SQ01 and Sq02

DATA : BEGIN OF itab OCCURS 0,

aufnr LIKE ekkn-aufnr,

END OF itab.

DATA : count TYPE i.

SELECT aufnr FROM ekkn INTO TABLE itab.

SORT itab BY aufnr .

LOOP AT itab.

IF NOT itab-aufnr IS INITIAL.

ADD 1 TO count.

AT END OF aufnr.

IF count GE 2.

WRITE:/'Production order',itab-aufnr,'Total Duplicated' &

'Records:', count.

CLEAR count.

else.

clear count.

WRITE:/'Production order',itab-aufnr,'Total Duplicated' &

'Records:', count.

ENDIF.

ENDAT.

ENDIF.

ENDLOOP.

Read only

0 Likes
1,130

Now it's working..even more than 3 requisitions...

check this...

REPORT test.

DATA : BEGIN OF itab OCCURS 0,

ebeln LIKE ekko-ebeln, "PO number

banfn LIKE eban-banfn, "Requisition number

count TYPE p DECIMALS 0,

END OF itab.

DATA : count(2) TYPE p.

DATA : no_of_records(3) TYPE p.

itab-banfn = '4500001258'.

itab-ebeln = '1031372'.

APPEND itab.

itab-banfn = '4500001259'.

itab-ebeln = '1031373'.

APPEND itab.

itab-banfn = '4500001260'.

itab-ebeln = '1031374'.

APPEND itab.

itab-banfn = '4500001261'.

itab-ebeln = '1031374'.

APPEND itab.

itab-banfn = '4500001262'.

itab-ebeln = '1031376'.

APPEND itab.

itab-banfn = '4500001263'.

itab-ebeln = '1031377'.

APPEND itab.

itab-banfn = '4500001264'.

itab-ebeln = '1031377'.

APPEND itab.

itab-banfn = '4500001265'.

itab-ebeln = '1031374'.

APPEND itab.

SORT itab BY ebeln.

LOOP AT itab.

ADD 1 TO count.

AT END OF ebeln.

IF count GE 2.

DO.

READ TABLE itab WITH KEY ebeln = itab-ebeln count = 0.

IF sy-subrc EQ 0.

itab-count = count.

MODIFY itab INDEX sy-tabix TRANSPORTING COUNT.

ELSE.

EXIT.

ENDIF.

ENDDO.

ELSE.

itab-count = count.

MODIFY itab INDEX sy-tabix TRANSPORTING count.

ENDIF.

CLEAR count.

ENDAT.

ENDLOOP.

LOOP AT itab.

WRITE 😕 itab-banfn, itab-ebeln, itab-count.

ENDLOOP.

Read only

0 Likes
1,130

DATA : BEGIN OF itab OCCURS 0,

aufnr LIKE ekkn-aufnr,

duplicate type i.

END OF itab.

DATA : count TYPE i.

SELECT aufnr FROM ekkn INTO TABLE itab.

SORT itab BY aufnr .

LOOP AT itab.

IF NOT itab-aufnr IS INITIAL.

ADD 1 TO count.

AT END OF aufnr.

IF count GE 2.

WRITE:/'Production order',itab-aufnr,'Total Duplicated' &

'Records:', count.

CLEAR count.

ENDIF.

itab-dupliacte = count.

modify itab.

ENDAT.

ENDIF.

ENDLOOP.

Now you will have the duplicate count for each order in itab.

Simply read this table itab with the order and populate the duplicates field in the other table.

Hope this helps.

Cheers

shivika

Edited by: Shivika Bhorchi on Jan 4, 2008 11:52 AM

Read only

0 Likes
1,130

Hi Shivika,

Error occurs saying Type "1" is no longer allowed in this Release.

Read only

0 Likes
1,130

it is type i not 1.

Read only

0 Likes
1,130

Try this...

DATA : BEGIN OF itab OCCURS 0,

aufnr LIKE ekkn-aufnr,

cnt type i,

END OF itab.

DATA : count TYPE i.

DATA : no_of_records TYPE i.

DATA : BEGIN OF itab1 OCCURS 0,

aufnr LIKE ekkn-aufnr,

duplicates type i,

END OF itab1.

SELECT aufnr FROM ekkn INTO corresponding fields of table itab1.

CLEAR no_of_records.

SELECT aufnr COUNT(*) FROM ekkn INTO table itab group by aufnr.

SORT itab BY aufnr.

write:/ 'Duplicate Records'.

LOOP AT itab1.

read table itab with key aufnr = itab1-aufnr.

itab1-duplicates = itab-cnt.

modify itab1.

write:/ itab1-aufnr, itab1-duplicates.

ENDLOOP.

Read only

0 Likes
1,130

Hi Guys, still not got what I want.

This code that Shivia has given is correct and exactly what I am looking for only problem is that it is using a write statmenet. The thing is that I have a query which runs and I want the data to be populated for the results in the query i have a field called duplicates and i want the information to look at the line in the query and post a result against the line i do not want a write statement the information should be displayed in the query this code is correct now how to a get the information across to the query line item instead of a report being displayed on exiting the query.

DATA : BEGIN OF itab OCCURS 0,

aufnr LIKE ekkn-aufnr,

END OF itab.

DATA : count TYPE i.

SELECT aufnr FROM ekkn INTO TABLE itab.

SORT itab BY aufnr .

LOOP AT itab.

IF NOT itab-aufnr IS INITIAL.

ADD 1 TO count.

AT END OF aufnr.

IF count GE 2.

WRITE:/'Production order',itab-aufnr,'Total Duplicated' &

'Records:', count.

CLEAR count.

else.

clear count.

WRITE:/'Production order',itab-aufnr,'Total Duplicated' &

'Records:', count.

ENDIF.

ENDAT.

ENDIF.

ENDLOOP.

Read only

0 Likes
1,130

hey

is your problem solved?

cheers

shivika

Read only

0 Likes
1,130

lol no please see previous post, my head is killing now. have you got an email address? i can send you screen shots of what im doing and want it might be helpful i think its straight forward but im struggling to explain it correctly

Read only

0 Likes
1,130

just throw a mail to kesav2005@rediffmail.com right now...i'll correct it for u...:-)

Read only

0 Likes
1,130

damn, somebody will have really deserved the ten points this time...