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

Поиск
Список
Период
Сортировка
Искать

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

От:
"Eric" <e-lz@263.net>
Дата:
��    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?

От:
Peter Eisentraut <peter_e@gmx.net>
Дата:
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?

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
>> 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?

От:
Robert Forsman <thoth@purplefrog.com>
Дата:
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);

FAQ