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: 

Check existence of data in internal table against actual table

JMorozowski
Active Participant
0 Kudos

Hi. I am very new to ABAP so please excuse me if I am using terminology incorrectly or asking a very simple question. I am working on a program that uploads data from a spreadsheet and inserts it into a table. I have the basics of it working quite well, but now I am trying to put in some checks before writing records to the table. For instance, first I am putting all of the records in the spreadsheet into an internal table. I would then like to check two of the fields to see if a plant and material combination exists in table MARC. So I am looping my internal table into a work area and I would like to use the fields from teh work area in a where clause. Is that possible? and if so how can I do that? I've tried to use the column numbers (col3 and col2), but when I run a syntax check it doesn't like that.

Thanks,

Jon

1 ACCEPTED SOLUTION

Jelena
Active Contributor

OK, so yes, it is a simple question and I'm honestly quite surprised you've managed to read a spreadsheet without mastering internal tables first.

Anyways... Since you're asking about MARC table I'm assuming you're using an ECC 6.0 system without HANA. If you're using HANA then disregard and check this blog for better suggestions.

It's not advisable to loop through the internal table entries and select data from the database within the loop. In this scenario I'd use the dreaded SELECT... FOR ALL ENTRIES, as Ravi was trying to suggest (albeit a bit clumsily). Make sure to read Help (F1 key) on that command and check beforehand that your internal table is not empty.

Use SELECT... INTO TABLE... and you'll have the result in an internal table. I'd suggest using HASHED table type for it with the same key as MARC primary key (find out why).

So, you'll have two internal tables: one with the spreadsheet data and one with the valid plant/material combinations. I'm guessing at this point you'd want to LOOP through the spreadsheet table, READ the material/plant table and then do something if the entry is not found (sy-subrc <> 0 after READ). If you do not actually need any data and are only checking for existence, then READ... TRANSPORTING NO FIELDS is a good choice. Read this blog to find about other options for this task.

If you are using ABAP 7.4 and higher then you can take advantage of inline declarations, otherwise you'll need to define something to LOOP into. There are examples available in the documentation. I'd encourage you to read the whole internal table section in ABAPDOCU, as suggested in the comments, because it's rather important.

8 REPLIES 8

former_member524141
Participant
0 Kudos

use below code .

Select matnr werks from Marc into itab1

 For all entries in itab_from_spreadsheet 

Where matnr eq itab_from_spreadsheet-matnr

And werks eq itab_from_spreadsheet-werks.

Loop at itab_from_spreadsheet.

read table itab with key   matnr eq itab_from_spreadsheet-matnr 
werks eq itab_from_spreadsheet-werks transporting no fields.

if sy-subrc eq 0.

// move the row from itab_from_spreadsheet to other internal table

endif.

Endloop.

Come on now... Even if you fixed the typo in the DELETE condition (and who says OP wants to delete?), this code would've just wiped out the whole "spreadsheet" table except for one material/plant combination.

And short form of LOOP AT... is obsolete. Please make more effort to check the accuracy when sharing code on SCN, going forward.

0 Kudos

jelena.perfiljeva thanks for pointing out and providing more accurate solution. SAP always has something new to learn.

Good Day!

Sandra_Rossi
Active Contributor

Please share your code (add it directly into your question, not a comment, not an answer, and please format the code with CODE button).

loyd_enochs3
Participant

Transaction ABAPDOCU can help you understand how to access and interrogate internal tables.

Execute transaction ABAPDOCU, and then use the following navigation:

ABAP - Reference

-> Processing Internal Data

-> Internal Tables

-> Processing Statements for Internal Tables

-> LOOP AT itab

Of course, the entire Internal Table topic will be informative as well.

Transaction ABAPDOCU is a valuable resource for new as well as experienced ABAPers, and I highly recommend becoming familiar with all the different ways to use it.

Jelena
Active Contributor

OK, so yes, it is a simple question and I'm honestly quite surprised you've managed to read a spreadsheet without mastering internal tables first.

Anyways... Since you're asking about MARC table I'm assuming you're using an ECC 6.0 system without HANA. If you're using HANA then disregard and check this blog for better suggestions.

It's not advisable to loop through the internal table entries and select data from the database within the loop. In this scenario I'd use the dreaded SELECT... FOR ALL ENTRIES, as Ravi was trying to suggest (albeit a bit clumsily). Make sure to read Help (F1 key) on that command and check beforehand that your internal table is not empty.

Use SELECT... INTO TABLE... and you'll have the result in an internal table. I'd suggest using HASHED table type for it with the same key as MARC primary key (find out why).

So, you'll have two internal tables: one with the spreadsheet data and one with the valid plant/material combinations. I'm guessing at this point you'd want to LOOP through the spreadsheet table, READ the material/plant table and then do something if the entry is not found (sy-subrc <> 0 after READ). If you do not actually need any data and are only checking for existence, then READ... TRANSPORTING NO FIELDS is a good choice. Read this blog to find about other options for this task.

If you are using ABAP 7.4 and higher then you can take advantage of inline declarations, otherwise you'll need to define something to LOOP into. There are examples available in the documentation. I'd encourage you to read the whole internal table section in ABAPDOCU, as suggested in the comments, because it's rather important.

JMorozowski
Active Participant
0 Kudos

Jelena,

Thanks for the detailed answer and sorry for the late acknowledgement!

Regards,

Jon

former_member1716
Active Contributor
0 Kudos

Hello Jon Morozowski,

I hope you are interested in only processing Valid entries from the spreadsheet, to check this validity you are considering that if the entries are present in MARC table then you decide the entry is Valid.

For this case first of all it is very important for us to look into your code to suggest any efficient solution. Kindly Use the CODE Button for pasting the code so that it will be easy for us to understand.

Now Coming to the solution Part, I can give high level overview of how your requirement can be satisfied. However with your code we can help you with identifying the mistakes in the code and steps to overcome it. Please note below steps are very BASIC for beginners to understand however with modern Syntax the code can be optimized even further.

1) Consider You have Internal table 1 say INT1, this table has all the fields from spread sheet along with material and Plant.

2) Am not sure what data types you have assigned to these fields in INT1, if it is string transfer the data to another internal table Say INT2 which has proper data types. for example MATNR for Material and WERKS for plant.

3) Now your Internal table INT2 has all the entries with proper data types, Pass these values to a temporary table and Write a Select Query on MARC table for all the entries in temporary table and fetch the value into another internal table INT3. Before applying for all entries ensure below two pre-requisites are done on temporary table:

a) Check for the table if it has entries, Key used in ABAP is "NOT INITIAL"

b) Sort the INT2 by MATNR and WERKS and DELETE ADJACENT DUPLICATES FROM INT2 COMPARING MATNR WERKS.

Through the above two checks we ensure there is no repeated entry is involved in the select Query.

4) Now you have two Tables, INT2 has all the data from spread sheet and INT3 has all the valid entries from MARC table based on data in INT2.

5) Now by using appropriate Read statement on INT3 for the data in INT2 you can find the entry is Valid (SY-SUBRC EQ 0) or invalid (SY-SUBRC NE 0). Ensure you use sort the INT3 table by MATNR and WERKS and use Binary search in the read statement.

Hope it helps! Do let us know for further clarification if needed!

Regards!