How to insert record only if primary key does not exist

Поиск
Список
Период
Сортировка
От Andrus
Тема How to insert record only if primary key does not exist
Дата
Msg-id 245B331DA82D498EAE7B90CFE5F469FE@dell2
обсуждение исходный текст
Ответы Re: How to insert record only if primary key does not exist  (Craig Ringer <ringerc@ringerc.id.au>)
Re: How to insert record only if primary key does not exist  (Edson Richter <edsonrichter@hotmail.com>)
Список pgsql-general
Table is defined as
 
CREATE TABLE firma1.klient (
  kood character(12) NOT NULL DEFAULT nextval('firma1.klient_kood_seq'::regclass),
....
);
 
How to insert record to this table only if primary key does not exist ?
I tried code below but got strange error as shown in log file.
Why this error occurs ?
 
Andrus.
 
2012-06-30 15:51:04 EEST ERROR:  duplicate key value violates unique constraint "klient_pkey"
2012-06-30 15:51:04 EEST DETAIL:  Key (kood)=(20037       ) already exists.
2012-06-30 15:51:04 EEST STATEMENT:  insert into klient (
    kood,
    nimi,
    tanav,
    piirkond,
    postiindek
    )
    select ((E'20037')), ((E'Statoil Fuel & Retail')), ((E'')), ((E'10148 nnn')),((E''))
    from klient
    where not exists (select 1 from klient where kood =((E'20037')))

В списке pgsql-general по дате отправления:

Предыдущее
От: Pavel Stehule
Дата:
Сообщение: Re: how to return results from code block
Следующее
От: Adrian Klaver
Дата:
Сообщение: Re: how to return results from code block