1

what i am try to do is something like this:

select 
case when (/* here should come sql statement that check if table exist */)
then 
 (select count(*) from my_external_table)
else 
  0 
end 

I know that theoretically i could use the SVV_ALL views , but it's sometimes it's cuasing the query to be slow ..

As an example for what i am looking for , this postgres query could be great for me , but Redshift do not support the to_regclass function:

select 
case when exist(select * from (select to_regclass('my_table') as rg) where rg IS NOT NULL )
then 
 (select count(*) from my_table)
else 
  0 
end 

1 Answer 1

0

Unless you want to use a stored procedure, I suggest splitting the logic into two queries.

With the first query checking the logic:

SELECT CASE
WHEN EXISTS(SELECT 1 FROM pg_catalog.svv_tables WHERE table_schema = 'yourschema' AND table_name = 'yourtable')
THEN TRUE
ELSE FALSE
END

And then executing your other query based on this result. In my case, I'm running the query in a Lambda function so it was easy to move the logic there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.