Thread: Built-in functions - does an obect exist...

Built-in functions - does an obect exist...

From
"Loftis, Charles E"
Date:
In many other db systems you can use built in functions to check to see if
an object exists in the db...

For example, in Sybase, you can use the object_id function to check to see
if an object exists...
  if object_id('some table, view, stored procedure, etc. name') is not null
     -- perform some action, like drop a stored proc
  else
     -- perform some other action
  End -- if


Does an equivalent function exist in Postgres?
Furthermore, where should I look to find the list of such built-in
functions?

Re: Built-in functions - does an obect exist...

From
Bruno Wolff III
Date:
On Tue, Jun 08, 2004 at 14:08:11 -0500,
  "Loftis, Charles E" <charles.loftis@eds.com> wrote:
> In many other db systems you can use built in functions to check to see if
> an object exists in the db...
>
> For example, in Sybase, you can use the object_id function to check to see
> if an object exists...
>   if object_id('some table, view, stored procedure, etc. name') is not null
>      -- perform some action, like drop a stored proc
>   else
>      -- perform some other action
>   End -- if
>
>
> Does an equivalent function exist in Postgres?
> Furthermore, where should I look to find the list of such built-in
> functions?

The system catalog and the information schema have metadata about objects.
You can query those tables to find out if an object with a particular name
currently exists. The logic for what to do depending on what is found
will probably need to be in your application or in a function. Straight
SQL is pretty limited for providing conditional code execution.