Re: shorter way to get new value of serial?
| От | Alban Hertroys |
|---|---|
| Тема | Re: shorter way to get new value of serial? |
| Дата | |
| Msg-id | 43819C92.1070302@magproductions.nl обсуждение |
| Ответ на | shorter way to get new value of serial? (Harald Armin Massa <haraldarminmassa@gmail.com>) |
| Ответы |
Re: shorter way to get new value of serial?
|
| Список | 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 по дате отправления: