Re: shorter way to get new value of serial? - Mailing list pgsql-general

From Alban Hertroys
Subject Re: shorter way to get new value of serial?
Date
Msg-id 43819C92.1070302@magproductions.nl
Whole thread Raw
In response to shorter way to get new value of serial?  (Harald Armin Massa <haraldarminmassa@gmail.com>)
Responses Re: shorter way to get new value of serial?  (Harald Armin Massa <haraldarminmassa@gmail.com>)
List pgsql-general
Harald Armin Massa wrote:
> I have a table:
> CREATE TABLE rechner
> (
>   id_r int4 NOT NULL DEFAULT nextval('rechner_id_r_seq'::regclass),
>   name text,
>   CONSTRAINT rechner_pkey PRIMARY KEY (id_r)
> )
> CREATE UNIQUE INDEX rechner_name
>   ON rechner
>   USING btree
>   (name);
>
> and want to have the existing or new id of 'newobjekt'
> CREATE OR REPLACE FUNCTION getrechnerid( text)
>   RETURNS int4 AS
> '        DECLARE
>             result int4;
>         BEGIN
>             select id_r from rechner where name=upper($1) into result;
>
>         IF not FOUND THEN
>        select nextval(''swcheck_id_check_seq'') into result;
>        insert into rechner (id_r, name) values (result, upper($1));

Why don't you just use the default? You could entirely do away with the
'result' variable that way:

CREATE OR REPLACE FUNCTION getrechnerid( text)
   RETURNS int4 AS
'   BEGIN
       select id_r from rechner where name=upper($1) into result;

       IF not FOUND THEN
       insert into rechner (name) values (upper($1));
       END IF;
...


--
Alban Hertroys
alban@magproductions.nl

magproductions b.v.

T: ++31(0)534346874
F: ++31(0)534346876
M:
I: www.magproductions.nl
A: Postbus 416
    7500 AK Enschede

//Showing your Vision to the World//

pgsql-general by date:

Previous
From: "Marc G. Fournier"
Date:
Subject: Test, ignore ...
Next
From: Harald Armin Massa
Date:
Subject: Re: shorter way to get new value of serial?