Search Postgresql Archives

Re: shorter way to get new value of serial?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxxxxxxxxx

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//


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux