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: 

SAP ABAP Query in same table to get different output

acetedi
Participant
0 Kudos
824

Hello expert

I have some problems with my query. I need to excluded output from table lt_regup with different query and then i used lt_regup2 to get output again.

this is my code

SELECT FROM regup
FIELDS belnr, laufd, laufi, vblnr, xvorl
FOR ALL ENTRIES IN @it_bsak2
WHERE belnr = @it_bsak2-belnr
  AND vblnr <> ' '
  AND xvrol = 'X'
INTO TABLE @lt_regup

output from lt_regup look like this

on the lt_regup2 i need excluded value lt_regup-belnr

current output in lt_regup2

       SELECT FROM regup                 "UNPOSTED<br>          FIELDS belnr , laufd , laufi , vblnr , xvorl<br>          FOR ALL ENTRIES IN @lt_regup<br>          WHERE belnr <> @lt_regup-belnr<br>           AND laufi = @lt_regup-laufi<br>           AND laufd = @lt_regup-laufd<br>           AND xvorl = 'X'<br>           AND vblnr <> ' '<br>           INTO TABLE @lt_regup2.

the result should not have belnr on lt_regup...

what problem with my query?

any guide or comment will be helpful

Thank You

5 REPLIES 5

Sandra_Rossi
Active Contributor
0 Kudos
720

With FOR ALL ENTRIES, you should use only = at all places where the for all entries table is involved.

This line is incorrect:

    WHERE belnr <> @lt_regup-belnr

FOR ALL ENTRIES is to be avoided as far as possible. It has been discussed a lot in the forum and in blog posts.

Can't you use joins or other alternatives? (where not exists, etc.)

Otherwise, just remove the line above from the SELECT, and filter out by classic ABAP code after the SELECT.

Jelena_Perfiljeva
Active Contributor
0 Kudos
720

It would be super helpful if you didn't use itab1/itab2 as a replacement for the words "internal table". It took a few moments to realize there are no itab1/itab2 in the code and you were actually referring to lt_regup and lt_regup2 from the code example. Good question is 80% of the answer. 🙂 I also have some words on the code not following Clean ABAP but that's another story.

I'm still fuzzy on what you mean by "excluded output from table regup (itab1) with different query and then i used regup(itab2) to get output again" but it seems like you're using FOR ALL ENTRIES but you actually want "except for these entries". Naturally, this command just doesn't work that way.

I'm not sure what is the best solution in this context (there could be a couple of options) but to exclude something based on rather complex WHERE criteria, subquery usually works the best and it has exactly the syntax for it.

If you're working with a HANA-based system, there are more options as well but I'm guessing it's not the case here.

0 Kudos
720

Thank you for respone my quesion Mrs Jelena..

I will fix my question and regarding clean ABAP i will consider to apply in my project thank u again for your some advice.

This question is resolved my using some condition in read table to get what output i need.

raymond_giuseppi
Active Contributor
0 Kudos
720

Did you notice that in run 20231102/UPDT1 there were 2 different document numbers in first internal table, so the second one is selected by the first one record and vice-versa in the FOR ALL ENTRIES clause.

Could you formulate your exclusion rule in plain logical English?

acetedi
Participant
0 Kudos
720

Thank you all for replying my question...

Mr Sandra Rossi is right this syntax is not correct

WHERE belnr <> @lt_regup-belnr

so I just deleted that and tried to figure it out in a different way....

what I did is just give some conditions in read table lt_regup2

Thank you it gave me some guidance not only in one way.

Also thank u for Mr Raymond. I just realized my be that not possible in the selected query and i tried to another way.