I am an SQL noob, and I have been trying to select data only if a table exists, and nothing if it doesn't. I've looked at various solutions, and this is the best I've been able to come up with:
DO $$
BEGIN
IF EXISTS
( SELECT 1
FROM information_schema.tables
WHERE table_schema = 'public'
AND table_name = "Test_Table"
)
THEN
SELECT
test_col
FROM "Test_Table"
ORDER BY time ASC
END IF ;
END
$$ ;
but this gives:
ERROR: syntax error at or near "END"
LINE 14: END IF ;
Note that the table name may contain uppercase characters, so I believe the double quotes are necessary.
Any ideas? Maybe trying to catch an exception would be a better approach?
Thanks!