Re: Verifying a timestamp is null or in the past

From: Alexander Farber <alexander(dot)farber(at)gmail(dot)com>
To: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Verifying a timestamp is null or in the past
Date: 2011-12-30 10:03:24
Message-ID: CAADeyWg33Bci2bOcMBRmsrxn1HcaN1hdKMFBF-_2B-qZ2FRwiQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hello again, please 1 more question:

can I have a SELECT statement inside of an IF-conditional?

The doc
http://www.postgresql.org/docs/8.4/static/plpgsql-control-structures.html
does not list such an example.

I'm asking, because I'd like to get rid of the has_vip
variable in my rewritten procedure below:

/* move 1 week of VIP-status from
player _from to player _to */

create or replace function pref_move_week(_from varchar,
_to varchar) returns void as $BODY$
declare
has_vip timestamp;
begin

select vip into has_vip from pref_users
where id=_from
and vip > current_timestamp + interval '1 week';

if (has_vip is NULL) then
return;
end if;

update pref_users set
vip = vip - interval '1 week'
where id=_from;

update pref_users set
vip = greatest(vip, current_timestamp) + interval '1 week'
where id=_to;

end;
$BODY$ language plpgsql;

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Cezariusz Marek 2011-12-30 11:04:46 How to get the time zone offset
Previous Message dhaval jaiswal 2011-12-30 06:35:59 streaming replication vacuum