Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 3E26747627C for ; Tue, 26 Nov 2002 06:55:42 -0500 (EST) Received: from fzkmail2.fzk.de (fzkmail2.fzk.de [141.52.27.52]) by postgresql.org (Postfix) with SMTP id 8F13747627B for ; Tue, 26 Nov 2002 06:55:40 -0500 (EST) Received: FROM resy5.fzk.de BY fzkmail2.fzk.de ; Tue Nov 26 12:55:37 2002 +0100 Received: œby rodos.fzk.de with ESMTP (8.8.6 (PHNE_17135)/8.7.1) id MAA26420; Tue, 26 Nov 2002 12:55:37 +0100 (MET) Message-ID: <3DE36138.2E8F24FF@rodos.fzk.de> Date: Tue, 26 Nov 2002 12:55:36 +0100 From: Christoph Haller X-Mailer: Mozilla 4.06 [en] (X11; I; HP-UX B.10.20 9000/780) MIME-Version: 1.0 To: nonsolosoft@diff.org Cc: pgsql-sql@postgresql.org Subject: Re: PL/SQL trouble Content-Type: text/plain; charset=iso-8859-2 Content-Transfer-Encoding: 7bit X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200211/365 X-Sequence-Number: 10507 > I really don't understand following PostgreSQL 7.2.3 behaviour: > > $ psql mydb > mydb=> CREATE FUNCTION MONDAY(timestamp) RETURNS DATE AS ' > DECLARE > var1 date; > BEGIN > select into var1 to_date($1::date-(case when extract(DOW from > timestamp $1) = 0 then 6 else (extract(DOW from timestamp $1)-1) end)); > RETURN var1; > END' > language 'plpgsql'; > > CREATE > mydb=> select MONDAY('now'::timestamp); > NOTICE: Error occurred while executing PL/pgSQL function MONDAY > NOTICE: line 4 at select into variables > ERROR: parser: parse error at or near "$2" > mydb=> \q > > But I've not inserted any $2 there. > I've rewritten the same function in other ways but I've got the same error. > Something like the following works (as Richard already pointed out): CREATE OR REPLACE FUNCTION MONDAY(timestamp) RETURNS DATE AS ' DECLARE ts_param ALIAS FOR $1; var1 date; BEGIN select into var1 to_date(ts_param::date- (case when extract(DOW from ts_param) = 0 then 6 else (extract(DOW from ts_param)-1) end),''DD''); RETURN var1; END' language 'plpgsql'; Me personally would prefer another approach: CREATE OR REPLACE FUNCTION MONDAY(timestamp) RETURNS DATE AS ' DECLARE ts_param ALIAS FOR $1; var1 date; var2 double precision; BEGIN var2 := extract(DOW from ts_param); IF var2 = 0 THEN var2 := 6; ELSE var2 := var2 - 1; END IF; var1 := to_date(ts_param::date - var2,''DD''); RETURN var1; END' language 'plpgsql'; because it's easier to read, but that's only a matter of taste I suppose. Regards, Christoph