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: 

How to check correctly for information in a transparent table?

Former Member
0 Kudos
272

Hello all,

I'm working on a development where I have a transparent table and the problem I'm having is more than a "correct way" than a real problem. You'll see, the table can have just one set of information for a user so before writing on it I must delete the old information the user has on it if such exists. I know I can make a simple DELETE directly to the table but doing so I will get a sy-subrc 4 in cases the user didn't have information before therefore I won't be able to know if a error occurred or simple there were not information on the table. The only solution I could came out with was to make a SELECT to the table and if it finds something then I delete. My question is then, If is there a better way to solve this issue and avoid using two statements?

I'm hoping I made my self clear enough if not please let me know so I can try to explain it better. I'll be waiting for your answers. Thanks in advance!

Regards.

1 ACCEPTED SOLUTION

0 Kudos
199

Hello there,

The method you chose is totally useful. But i may show you another way around

You can use the keyword 'insert' when you try to put some records into your Z transparent table. If there are no records in the Z table that has the same values on their 'key' fields with the values on the work area that you use in your program ; the information that you want to save to that Z table will be inserted to the table without any problems.

But if the sy-subrc value returns 4 then you can understand that there was a line in that Z table already that has the same values in their key fields. So that you can catch this with checking up sy-subrc in your code and take the actions to either modify that line in the table or do nothing at all. I'll try to give some code example just to make it clear.

Assume that zdb_tab is the transparent table's name.

data: ls_tab like zdb_tab.               "the work area must have the same structure with the table

insert into zdb_tab values ls_tab.
if sy-subrc eq 0.

     message 'No problem' type 'S'.

elseif sy-subrc eq 4.

     message 'There was a line in the Z table that was modified' type 'S'.

     modify zdb_tab from ls_tab.   

endif

Please note that modifying a transparent table can be used for either updating a record or inserting a new one to the table. I just used insert because i thought you would like to know whether there was a record in the table before the update or not.

I hope that helps,
Regards,
Arman

6 REPLIES 6

0 Kudos
200

Hello there,

The method you chose is totally useful. But i may show you another way around

You can use the keyword 'insert' when you try to put some records into your Z transparent table. If there are no records in the Z table that has the same values on their 'key' fields with the values on the work area that you use in your program ; the information that you want to save to that Z table will be inserted to the table without any problems.

But if the sy-subrc value returns 4 then you can understand that there was a line in that Z table already that has the same values in their key fields. So that you can catch this with checking up sy-subrc in your code and take the actions to either modify that line in the table or do nothing at all. I'll try to give some code example just to make it clear.

Assume that zdb_tab is the transparent table's name.

data: ls_tab like zdb_tab.               "the work area must have the same structure with the table

insert into zdb_tab values ls_tab.
if sy-subrc eq 0.

     message 'No problem' type 'S'.

elseif sy-subrc eq 4.

     message 'There was a line in the Z table that was modified' type 'S'.

     modify zdb_tab from ls_tab.   

endif

Please note that modifying a transparent table can be used for either updating a record or inserting a new one to the table. I just used insert because i thought you would like to know whether there was a record in the table before the update or not.

I hope that helps,
Regards,
Arman

0 Kudos
199

I'll keep on it then and unfortunately the key is not that important in this specific case because the point is if there's or there's not data related to a specific user before inserting new information but what you explain sounds pretty good as well I'll try it when I have some time.

Thanks a lot!

kakshat
Product and Topic Expert
Product and Topic Expert
0 Kudos
199

Hi Jose,

Well, if your table would always have a maximum of only one row per user, you can use the MODIFY statement. With that, if there's a record available for the user, it will be updated with the new values and on the other hand, if there's no record available for that user, a new record will be inserted!

Regards,

Akshat

Former Member
0 Kudos
199

Hi Kumar, that's not the case the table would probably have thousands of rows per user what I tried to explain was that if a user already has data on the table this must be deleted from the table before inserting new data and it's not like updating information because the incoming data will most probably be different even on its key. Either way, thanks for the help I'm pretty sure it will be useful in the near future.

Thanks!

manukapur
Active Participant
0 Kudos
199

Read the table and modify the field rather than deleting and updating.

Former Member
0 Kudos
199

Hello Jose,

If you do not need to update a secondary index in the table, I suggest using the 'MODIFY' statement instead of the 'INSERT' statement.

The 'MODIFY' statement will insert rows if the key does not yet exist and change rows that have the same key.

Regards,

Kim