What is the problem with this query?

Started by Igor Korotover 8 years ago6 messagesgeneral
Jump to latest
#1Igor Korot
ikorot01@gmail.com

Hi, ALL,

draft=# IF NOT EXISTS( SELECT 1 FROM pg_class c, pg_namespace n WHERE
n.oid = c.relnamespace AND c.relname = 'abcattbl_tnam_ownr' AND
n.nspname = 'public' ) THEN CREATE INDEX "abcattbl_tnam_ownr" ON
"abcattbl"("abt_tnam" ASC, "abt_ownr" ASC);
ERROR: syntax error at or near "IF"
LINE 1: IF NOT EXISTS( SELECT 1 FROM pg_class c, pg_namespace n WHER...
^

Is "IF" operator not supported by PostgreSQL?

Thank you.

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#2David G. Johnston
david.g.johnston@gmail.com
In reply to: Igor Korot (#1)
Re: What is the problem with this query?

On Sunday, July 23, 2017, Igor Korot <ikorot01@gmail.com> wrote:

Is "IF" operator not supported by PostgreSQL

IF is pl/pgsql, not SQL.

David J.

#3Igor Korot
ikorot01@gmail.com
In reply to: David G. Johnston (#2)
Re: What is the problem with this query?

Hi, David,

On Sun, Jul 23, 2017 at 5:07 PM, David G. Johnston
<david.g.johnston@gmail.com> wrote:

On Sunday, July 23, 2017, Igor Korot <ikorot01@gmail.com> wrote:

Is "IF" operator not supported by PostgreSQL

So how do I write this properly?

Thank you.

IF is pl/pgsql, not SQL.

David J.

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#4Christoph Moench-Tegeder
cmt@burggraben.net
In reply to: Igor Korot (#3)
Re: What is the problem with this query?

## Igor Korot (ikorot01@gmail.com):

Is "IF" operator not supported by PostgreSQL

So how do I write this properly?

There is documentation for that:
https://www.postgresql.org/docs/current/static/sql-createindex.html

Regards,
Christoph

--
Spare Space

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#5Igor Korot
ikorot01@gmail.com
In reply to: Christoph Moench-Tegeder (#4)
Re: What is the problem with this query?

Hi, Christoph,

On Sun, Jul 23, 2017 at 5:30 PM, Christoph Moench-Tegeder
<cmt@burggraben.net> wrote:

## Igor Korot (ikorot01@gmail.com):

Is "IF" operator not supported by PostgreSQL

So how do I write this properly?

There is documentation for that:
https://www.postgresql.org/docs/current/static/sql-createindex.html

Except that IF NOT EXIST clause is introduced in 9.5.

And if the server is <= 9.4?

I tried to use a solution from here:
https://dba.stackexchange.com/questions/35616/create-index-if-it-does-not-exist.
but apparently I fail.

Thank you.

Regards,
Christoph

--
Spare Space

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#6David G. Johnston
david.g.johnston@gmail.com
In reply to: Igor Korot (#3)
Re: What is the problem with this query?

On Sunday, July 23, 2017, Igor Korot <ikorot01@gmail.com> wrote:

Hi, David,

On Sun, Jul 23, 2017 at 5:07 PM, David G. Johnston
<david.g.johnston@gmail.com <javascript:;>> wrote:

On Sunday, July 23, 2017, Igor Korot <ikorot01@gmail.com <javascript:;>>

wrote:

Is "IF" operator not supported by PostgreSQL

So how do I write this properly?

CREATE FUNCTION or a DO block. See docs for usage.

Since the language is pl/pgsql you can learn how to write it here.

https://www.postgresql.org/docs/9.6/static/plpgsql.html

David J.