Skip to main content

Hello Community,

i have the challenge to extract and transform data from various SAP System und to combine them.

My problem is, that there are tables who are filled or not depending on the system. If not filled, there is also no table in the schema. So I get an error while running the SQL script.

My question: Is there an option to check with the SQL script if the table exists or not.

In my example below: How to check if TABLE1 is existing or not. If yes all fine, if no only run the TABLE2 part.

Thank you

Thomas

 

CREATE VIEW TEST_VIEW AS

SELECT

"TABLE1"."MANDT" AS MANDT,

"TABLE1"."TKNUM" AS TKNUM,

"TABLE1"."TSNUM" AS TSNUM

FROM "TABLE1"



UNION ALL



SELECT

"TABLE2"."MANDT" AS MANDT,

"TABLE2"."TKNUM" AS TKNUM,

"TABLE2"."TSNUM" AS TSNUM,

FROM "TABLE2"

 

This is what you are looking for

SELECT * FROM "TABLE1" WHERE EXISTS ( SELECT 1 FROM ALL_TABLES WHERE table_name = 'TABLE1' )

UNION ALL

SELECT * FROM TABLE2 WHERE NOT EXISTS ( SELECT 1 FROM ALL_TABLES WHERE table_name = 'TABLE2' );

 

Do SELECT * FROM ALL_TABLES because other schemas may have tables with the same name, so your WHERE clause should be adjusted accordingly.


Reply