pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Kenneth Marshall <ktm@rice.edu>
To: pgsql-sql@postgresql.org
Subject: How to get CURRENT_DATE in a pl/pgSQL function
Date: Tue, 18 May 2010 14:08:32 -0500
Message-ID: <20100518190832.GQ3892@aart.is.rice.edu> (raw)

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



view thread (11+ messages)  latest in thread

Message-ID: <20100518190832.GQ3892@aart.is.rice.edu>
Permalink:  ../20100518190832.GQ3892@aart.is.rice.edu/
Also on:    postgresql.org/message-id/20100518190832.GQ3892@aart.is.rice.edu

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: ktm@rice.edu
  Subject: Re: How to get CURRENT_DATE in a pl/pgSQL function
  In-Reply-To: <20100518190832.GQ3892@aart.is.rice.edu>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox