Defining and Using variables in a postgres function

From: "Harpreet Dhaliwal" <harpreet(dot)dhaliwal01(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Defining and Using variables in a postgres function
Date: 2007-02-01 17:09:05
Message-ID: d86a77ef0702010909m59f6b656u141238224ba4d447@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I have a function like the follwoing:

CREATE OR REPLACE FUNCTION sp_insert_raw_email(bool, text, text, text,
int4,text,text,text,text,text,text,text,timestamp)
RETURNS void AS
$BODY$
BEGIN
-- SELECT STATEMENT GOES HERE--
INSERT INTO tbl_email(option_public,
agency , id)
VALUES ($1,$2) ;
END;
$BODY$
LANGUAGE 'plpgsql' VOLATILE;

For inserting the id, i need to query a table xyz, fetch the maximum id in
it, increment it by 1 and store it in tbl_email.
Right after BEGIN in my function I have a commnet where in I need to query
the xyz table, fetch the max id and store it in a variable and then I can
increment this variable and store it in tbl_email.

How should i define this variable first and how to push the result of the
query fired on table xyz.

Thanks in advance,
~Harpreet

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tony Caduto 2007-02-01 17:13:25 Re: I "might" have found a bug on 8.2.1 win32
Previous Message George Pavlov 2007-02-01 17:08:45 Re: Subqueries - performance and use question