Re: IF NOT EXIST - Mailing list pgsql-general

From Pavel Stehule
Subject Re: IF NOT EXIST
Date
Msg-id CAFj8pRBAYy1C1oCRpm=U=t-Z6LyuvJH27M3SytOC9QX4gWH_7w@mail.gmail.com
Whole thread Raw
In response to IF NOT EXIST  (Igor Korot <ikorot01@gmail.com>)
List pgsql-general
Hi

út 18. 12. 2018 v 7:11 odesílatel Igor Korot <ikorot01@gmail.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.

pgsql-general by date:

Previous
From: Igor Korot
Date:
Subject: IF NOT EXIST
Next
From: Bret Stern
Date:
Subject: Format an Update with calculation