X-Original-To: pgsql-sql@postgresql.org Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 7A2C24758DC for ; Sat, 22 Feb 2003 19:15:21 -0500 (EST) Received: from postgresql.org ([64.49.215.8]) by localhost (postgresql.org [64.49.215.8:10024]) (amavisd-new) with SMTP id 55268-01 for ; Sat, 22 Feb 2003 19:15:10 -0500 (EST) Received: from spike.esolution.pl (spike.esolution.pl [217.11.134.154]) by postgresql.org (Postfix) with ESMTP id F24E4474E4F for ; Sat, 22 Feb 2003 19:15:09 -0500 (EST) Received: from vu208.neoplus.adsl.tpnet.pl ([80.50.140.208] helo=depesz.pl) by spike.esolution.pl with esmtp (TLSv1:DES-CBC3-SHA:168) (Exim 4.12) id 18mjnO-0008Gy-00 for pgsql-sql@postgresql.org; Sun, 23 Feb 2003 01:15:07 +0100 Received: from depesz by depesz.pl with local (Exim 4.12) id 18mjn0-0005fF-00 for pgsql-sql@postgresql.org; Sun, 23 Feb 2003 01:14:42 +0100 Date: Sun, 23 Feb 2003 01:14:42 +0100 From: Hubert depesz Lubaczewski To: pgsql-sql@postgresql.org Subject: how to return ONE single record from a function in plpgsql? Message-ID: <20030223001442.GA17740@depesz.pl> Reply-To: depesz@depesz.pl Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-2 Content-Disposition: inline User-Agent: Mutt/1.4i X-info-en-1: this message *may* reflect my personal opinion. it is *not* X-info-en-2: intended to reflect those of my employer, or anyone else. X-info-pl-1: wszelkie opinie =?iso-8859-2?Q?wyra=BFone_?= =?iso-8859-2?Q?w_tym_li=B6cie_prezentuj=B1_wy=B3=B1cznie_pogl=B1dy?= X-info-pl-2: autora listu. opinie =?iso-8859-2?Q?te_?= =?iso-8859-2?Q?w_=BFadnym__razie_nie__wyra=BFaj=B1__pogl=B1d=F3w?= X-info-pl-3: pracodawcy autora =?iso-8859-2?Q?listu_?= =?iso-8859-2?Q?_ani__innych__zwi=B1zanych__z__nim__os=F3b=2E?= X-Operating-System: Linux 2.4.18-xfs-1.1 i686 i686 X-Spam-Score: -1.2 (/) X-Scanner: exiscan for exim4 (http://duncanthrax.net/exiscan/) with mks-vir (http://www.mks.com.pl) *18mjnO-0008Gy-00*QYRdmwommh6* X-Virus-Scanned: by amavisd-new X-Archive-Number: 200302/635 X-Sequence-Number: 12033 hi i was thinking about using something like: CREATE type helper_login as (logged bool, username TEXT, points INT8, first_login bool, admin bool, last_login timestamptz, can_ask_questions bool, timeleft INTERVAL); CREATE OR REPLACE FUNCTION login(TEXT, TEXT, INT8) RETURNS helper_login AS ' DECLARE in_username ALIAS FOR $1; in_password ALIAS FOR $2; in_quiz_id ALIAS FOR $3; reply helper_login; BEGIN reply.logged := true; reply.username := in_username; reply.points := 0; reply.first_login := true; reply.admin := false; reply.last_login := now(); reply.can_ask_questions := false; reply.timeleft := ''1 hour''::INTERVAL; RETURN reply; END; ' LANGUAGE 'plpgsql'; but it doesn't work: > select login('depesz','dupa','1'); WARNING: plpgsql: ERROR during compile of login near line 15 ERROR: return type mismatch in function returning tuple at or near "reply" of course the code as it is now is not very usable, but this is just a test, on how to achieve what i'd like to. then i tried to make it work as: select * from login(...) and returning single row: CREATE OR REPLACE FUNCTION login(TEXT, TEXT, INT8) RETURNS setof helper_login AS ' DECLARE in_username ALIAS FOR $1; in_password ALIAS FOR $2; in_quiz_id ALIAS FOR $3; reply helper_login; BEGIN reply.logged := true; reply.username := in_username; reply.points := 0; reply.first_login := true; reply.admin := false; reply.last_login := now(); reply.can_ask_questions := false; reply.timeleft := ''1 hour''::INTERVAL; RETURN NEXT reply; RETURN; END; ' LANGUAGE 'plpgsql'; > select * from login('depesz','dupa','1'); WARNING: plpgsql: ERROR during compile of login near line 15 ERROR: Incorrect argument to RETURN NEXT at or near "reply" hmm .. ok. so it has to be record "inside". let's see: i modified definition from reply helper_login; to reply record; this time i got: > select * from login('depesz','dupa','1'); WARNING: Error occurred while executing PL/pgSQL function login WARNING: line 7 at assignment ERROR: record "reply" is unassigned yet - don't know its tuple structure so - is there any chance to return one row which i dont get by a more or less complicated "select" but rather in a computational way? best regards hubert depesz lubaczewski -- hubert depesz lubaczewski http://www.depesz.pl/ i choose to hate people when they're not polite; bruise me; that's allright. bananafishbones "pow wow"