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: 

If else based on tables in AMDP

former_member182337
Participant
0 Kudos

Hello,

I need to compare three tables and determine a field from it i.e if the field is available in the table 1, retrieve the same else retrieve from table 2 or else from table 3.

Can someone give me pointers as to how to arrive at this logic?

Thank you.

Regards,

Prem

5 REPLIES 5

pranay570708
Active Contributor
0 Kudos

Do something like below:

select <field> from table1 into lv_field where <specify conditions>.

if sy-subrc NE 0.

select <field> from table2 into lv_field where <specify conditions>.

if sy-subrc NE 0.


select <field> from table3 into lv_field where <specify conditions>.


endif.

endif.


0 Kudos

Hello,

The logic is to be implemented in AMDP.

Regards,

Prem

amol_samte
Contributor
0 Kudos

Hi,

Based on your explanation I am providing below way...


prem kumar wrote:

I need to compare three tables and determine a field from it i.e if the field is available in the table 1, retrieve the same else retrieve from table 2 or else from table 3.

declare field nvarchar(100);

select field_name into field

FROM "PUBLIC"."TABLE_COLUMNS"

WHERE ("SCHEMA_NAME" = 'SCHEMANAME' AND "TABLE_NAME" = 'TABLENAME' AND COLUMN_NAME = 'FIELD');

if :field_name = 'FIELD' Then

else

end if;

-Amol S

0 Kudos

Thanks Amol.

Actually, I am wrong. I have not documented the question correctly. Sorry for that. My requirement is to retrieve data from the table 1 based on few conditions , if table 1 has data then retrieve the same and stop further processing. If there is no data , then check if table 2 has data else table 3.

I did some analysis and saw few codes where all the tables where joint and then using ifnull(tabl1.field, tabl2.field) the data gets retrieved.

Is this the only option? Won't it affect the performance if I join all the tables when there is no need to join each and every one of them?

Thank you.

Regards,

Prem

0 Kudos

Hi,


prem kumar wrote:

My requirement is to retrieve data from the table 1 based on few conditions , if table 1 has data then retrieve the same and stop further processing. If there is no data , then check if table 2 has data else table 3.

Yes we can write it in different way too.. without joining all tables together....

Since I don't know complete business logic of your side.

Might be below query will solve the issue..


declare lv_count nvarchar(1000);

it_table1 = select * from Table1 ;

select count(*) from :it_table1 into :lv_count;

if :lv_count = 0 then

it_table2 = select * from Table2;

select count(*) from :it_table2 into :lv_count;

if :lv_count = 0 then

it_table3 = select * from table3;

end if;

end if;

-Amol S