Thread: SQL( "if ...exists...),how to do it in the PostgreSQL?

SQL( "if ...exists...),how to do it in the PostgreSQL?

From
"Eric"
Date:
��    I can realize this function in the SYBase,but How can i do it in the PostgreSQL? 
/****SQL***/
if not exists(select id from test) insert into test(id) values (280);
/*********/ 

_____________________________________________
������Ʒ�����У��� http://shopping.263.net/category21.htm
��ƷС�ҵ�ӭ������ http://shopping.263.net/category23.htm


Re: SQL( "if ...exists...),how to do it in the PostgreSQL?

From
Peter Eisentraut
Date:
Eric writes:

>    I can realize this function in the SYBase,but How can i do it in the PostgreSQL?
>
> /****SQL***/
> if not exists(select id from test) insert into test(id) values (280);
> /*********/

Write a function in PL/pgSQL.

-- 
Peter Eisentraut   peter_e@gmx.net   http://funkturm.homeip.net/~peter



Re: SQL( "if ...exists...),how to do it in the PostgreSQL?

From
Tom Lane
Date:
>> if not exists(select id from test) insert into test(id) values (280);

> Write a function in PL/pgSQL.

That particular case could be handled like so:

insert into test(id) select 280 where not exists(select id from test);

The select produces either zero or one row depending on whether its
WHERE is true.  Voila, problem solved.  It's even nearly standard ;-)
although in something like Oracle you'd have to add "from dual", I
think.
        regards, tom lane


Re: SQL( "if ...exists...),how to do it in the PostgreSQL?

From
Robert Forsman
Date:
Eric writes:

>    I can realize this function in the SYBase,but How can i do it in the PostgreSQL?
>
> /****SQL***/
> if not exists(select id from test) insert into test(id) values (280);
> /*********/

I don't know if this is SQL92, but 

insert into test(id) select 280 where not exists(select id from test);