pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
IF NOT EXIST
4+ messages / 4 participants
[nested] [flat]

* IF NOT EXIST
@ 2018-12-18 06:10 Igor Korot <ikorot01@gmail.com>
  2018-12-18 06:40 ` Re: IF NOT EXIST Pavel Stehule <pavel.stehule@gmail.com>
  2018-12-18 08:02 ` Re: IF NOT EXIST Alban Hertroys <haramrae@gmail.com>
  0 siblings, 2 replies; 4+ messages in thread

From: Igor Korot @ 2018-12-18 06:10 UTC (permalink / raw)
  To: pgsql-general

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?

Thank you.




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: IF NOT EXIST
  2018-12-18 06:10 IF NOT EXIST Igor Korot <ikorot01@gmail.com>
@ 2018-12-18 06:40 ` Pavel Stehule <pavel.stehule@gmail.com>
  1 sibling, 0 replies; 4+ messages in thread

From: Pavel Stehule @ 2018-12-18 06:40 UTC (permalink / raw)
  To: Igor Korot <ikorot01@gmail.com>; +Cc: 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.
>
>

^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: IF NOT EXIST
  2018-12-18 06:10 IF NOT EXIST Igor Korot <ikorot01@gmail.com>
@ 2018-12-18 08:02 ` Alban Hertroys <haramrae@gmail.com>
  2018-12-18 13:53   ` Re: IF NOT EXIST Adrian Klaver <adrian.klaver@aklaver.com>
  1 sibling, 1 reply; 4+ messages in thread

From: Alban Hertroys @ 2018-12-18 08:02 UTC (permalink / raw)
  To: Igor Korot <ikorot01@gmail.com>; +Cc: pgsql-general


> On 18 Dec 2018, at 7:10, Igor Korot <ikorot01@gmail.com> wrote:
> 
> 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?

It looks like you’re trying to create a function unless it already exists, but you’re missing several important parts.

Firstly, the statement you’re looking for is CREATE OR REPLACE FUNCTION. Look it up in the docs for the various syntax options and for how to use it, as…

Secondly, your function doesn’t have a name. A function requires a name, or you wouldn’t ever be able to call it.
You will also have to specify a return type (functions return values) and the language the function is implemented in. The documentation will show you that there are several options you can provide too.

And lastly, a function requires an implementation.

Regards,

Alban Hertroys
--
There is always an exception to always.




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: IF NOT EXIST
  2018-12-18 06:10 IF NOT EXIST Igor Korot <ikorot01@gmail.com>
  2018-12-18 08:02 ` Re: IF NOT EXIST Alban Hertroys <haramrae@gmail.com>
@ 2018-12-18 13:53   ` Adrian Klaver <adrian.klaver@aklaver.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Adrian Klaver @ 2018-12-18 13:53 UTC (permalink / raw)
  To: Alban Hertroys <haramrae@gmail.com>; Igor Korot <ikorot01@gmail.com>; +Cc: pgsql-general

On 12/18/18 12:02 AM, Alban Hertroys wrote:
> 
>> On 18 Dec 2018, at 7:10, Igor Korot <ikorot01@gmail.com> wrote:
>>
>> 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?
> 
> It looks like you’re trying to create a function unless it already exists, but you’re missing several important parts.
> 
> Firstly, the statement you’re looking for is CREATE OR REPLACE FUNCTION. Look it up in the docs for the various syntax options and for how to use it, as…
> 
> Secondly, your function doesn’t have a name. A function requires a name, or you wouldn’t ever be able to call it.

With an exception:

https://www.postgresql.org/docs/10/sql-do.html

> You will also have to specify a return type (functions return values) and the language the function is implemented in. The documentation will show you that there are several options you can provide too.
> 
> And lastly, a function requires an implementation.
> 
> Regards,
> 
> Alban Hertroys
> --
> There is always an exception to always.
> 
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com




^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2018-12-18 13:53 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-12-18 06:10 IF NOT EXIST Igor Korot <ikorot01@gmail.com>
2018-12-18 06:40 ` Pavel Stehule <pavel.stehule@gmail.com>
2018-12-18 08:02 ` Alban Hertroys <haramrae@gmail.com>
2018-12-18 13:53   ` Adrian Klaver <adrian.klaver@aklaver.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox