Skip site navigation (1) Skip section navigation (2)

Re: INSERT [IGNORE] INTO TABLE

From: Najm Hashmi <najm(at)mondo-live(dot)com>
To: Edmar Wiggers <edmar(at)brasmap(dot)com>, "pgsql-sql(at)postgresql(dot)org" <pgsql-sql(at)postgresql(dot)org>
Subject: Re: INSERT [IGNORE] INTO TABLE
Date: 2000-11-01 17:56:26
Message-ID: 3A00594A.AA3A3120@mondo-live.com (view raw or flat)
Thread:
Lists: pgsql-sql
Edmar Wiggers wrote:

> >       If I try to insert some row which in the table already exist the
> >       postgre don't insert it and tell some error. Everything ok.
> >       I'll insert only if there is not the same (same key) record,
> >       else don't insert and don't tell me errors about it.
> >       In MySQL is a switch [IGNORE].
>
> Not a good feature for me. What do you do if the record already exists?
> Update it?
>
> Check existence and then insert or update. If you want, I guess you could
> wrap that inside a stored procedure.

Hi, here is an example of using  function using pl/pgsql for inserting and
checking whether an instance exists or not.....
CREATE FUNCTION add_new_user(text,text) RETURNS bool AS' DECLARE
oldUser RECORD;
USR ALIAS FOR $1;
PWORD ALIAS FOR $2;

BEGIN
SELECT INTO oldUser *
FROM users
where username=USR AND password= PWORD;
IF FOUND
 THEN
      RETURN ''f'';
ELSE
 INSERT INTO USERS(username,password)
     values(USR,PWORD);
     RETURN ''t'';
END IF;

END;'
LANGUAGE 'plpgsql';

Regards.
Najm


In response to

Responses

pgsql-sql by date

Next:From: Petter ReinholdtsenDate: 2000-11-02 02:13:59
Subject: Timespan / interval calculations?
Previous:From: Josh BerkusDate: 2000-11-01 17:01:28
Subject: Re: Outer Joins

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group