Re: IF NOT EXIST

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Igor Korot <ikorot01(at)gmail(dot)com>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: IF NOT EXIST
Date: 2018-12-18 06:40:32
Message-ID: CAFj8pRBAYy1C1oCRpm=U=t-Z6LyuvJH27M3SytOC9QX4gWH_7w@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi

út 18. 12. 2018 v 7:11 odesílatel Igor Korot <ikorot01(at)gmail(dot)com> napsal:

> Hi, ALL,
> I have a following statement:
>
> IF NOT EXIST( SELECT 1 SELECT 1 FROM pg_proc AS proc, pg_namespace AS
> ns ) CREATE FUNCTION();
>
> Unfortunately trying to execute it thru the ODBC interface with:
>
> ret = SQLExecDirect( m_hstmt, query, SQL_NTS );
>
> gives syntax error near IF.
>
> What is the proper way to do that?
>

IF is only plpgsql statement - so you cannot to call it from SQL
environment.

CREATE FUNCTION is not expensive command, and you can replace existing
function by statement

CREATE OR REPLACE FUNCTION ...

This is usual solution. You cannot to use in SQL IF statement ever in
PostgreSQL - This T-SQL syntax is not supported.

But lot of DDL statements has integrated IF

DROP TABLE IF EXISTS ...
CREATE TABLE IF NOT EXISTS ...
CREATE OR REPLACE FUNCTION ...

second solution is using SQL command DO - inside is plpgsql code, and you
can use conditional statement like IF, or other.

regards

Pavel

>
> Thank you.
>
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Bret Stern 2018-12-18 07:14:57 Format an Update with calculation
Previous Message Igor Korot 2018-12-18 06:10:48 IF NOT EXIST