opensource-developer
opensource-developer

Reputation: 3058

handle relation not found in postgresql

I trying to write a query in which for now the relation does not exists, there are many tables invovled in the sub query and 1 of them does not exists as of now.

Is there a way i can write the query so that it returns 0 in case the relation does not exists

for example my query is "Select emp_name from employee AS name limit 1"

is there a way to rewrite it to return 0 or null is the relation does not exists?

Any help would be appreciated. Thanks.

Upvotes: 0

Views: 67

Answers (1)

Craig Ringer
Craig Ringer

Reputation: 324851

The only way to do this is with dynamic SQL, either in the client or in plpgsql.

You can build your query using queries against information_schema to see which tables exist, then pass it to PL/PgSQL's EXECUTE to actually run the assembled query.

The format function's %I format specifier will be useful to you, as will the USING option to EXECUTE.

Upvotes: 1

Related Questions