Re: shorter way to get new value of serial?

From: Alban Hertroys <alban(at)magproductions(dot)nl>
To: Harald Armin Massa <haraldarminmassa(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: shorter way to get new value of serial?
Date: 2005-11-21 10:08:18
Message-ID: 43819C92.1070302@magproductions.nl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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(at)magproductions(dot)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//

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Harald Armin Massa 2005-11-21 10:13:04 Re: shorter way to get new value of serial?
Previous Message Marc G. Fournier 2005-11-21 06:41:27 Test, ignore ...