How to get CURRENT_DATE in a pl/pgSQL function

From: Kenneth Marshall <ktm(at)rice(dot)edu>
To: pgsql-sql(at)postgresql(dot)org
Subject: How to get CURRENT_DATE in a pl/pgSQL function
Date: 2010-05-18 19:08:32
Message-ID: 20100518190832.GQ3892@aart.is.rice.edu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

I am trying to write a function that updates the
date column to the current date. According to:

http://www.postgresql.org/docs/8.4/static/functions-datetime.html#FUNCTIONS-DATETIME-CURRENT

you can use CURRENT_DATE. When I try to use it in
the following pl/pgSQL function it gives the error:

ERROR: date/time value "current" is no longer supported
CONTEXT: PL/pgSQL function "merge_data" line 4 at assignment

Here is the code I am using:

CREATE FUNCTION merge_data(key INT, i INT) RETURNS
VOID AS
$$
DECLARE
curtime date;
BEGIN
curtime := 'CURRENT_DATE';
LOOP
-- first try to update the key
UPDATE data SET count = i, date = curtime WHERE k = key;
IF found THEN
RETURN;
END IF;
-- not there, so try to insert the key
-- if someone else inserts the same key concurrently,
-- we could get a unique-key failure
BEGIN
INSERT INTO data(k, count, date) VALUES (key, i, curtime);
RETURN;
EXCEPTION WHEN unique_violation THEN
-- do nothing, and loop to try the UPDATE again
END;
END LOOP;
END;
$$
LANGUAGE plpgsql;

It looks like it is keying on the CURRENT and dropping the _DATE piece.
I suspect that I am doing something wrong but I am not able to find a
pointer in the docs or mailing lists. Any help would be greatly appreciated.

Regards,
Ken

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Richard Broersma 2010-05-18 19:12:46 Re: How to get CURRENT_DATE in a pl/pgSQL function
Previous Message Torsten Zühlsdorff 2010-05-17 08:57:08 Re: [Trigger] Help needed with NEW.* and TG_TABLE_NAME