Received: from post.webmailer.de (natpost.webmailer.de [192.67.198.65]) by postgresql.org (Postfix) with ESMTP id B542D47596A for ; Thu, 11 Jul 2002 02:37:07 -0400 (EDT) Received: from ianb (pD9EB105A.dip.t-dialin.net [217.235.16.90]) by post.webmailer.de (8.9.3/8.8.7) with ESMTP id IAA28301; Thu, 11 Jul 2002 08:36:58 +0200 (MET DST) Content-Type: text/plain; charset="iso-8859-1" From: Ian Barwick To: "David Durst" Subject: Re: Insert Function Date: Thu, 11 Jul 2002 08:38:27 +0200 X-Mailer: KMail [version 1.4] References: <200207102245.44221.barwick@gmx.net> <1274.10.1.1.39.1026335078.squirrel@www.la-rubber.com> In-Reply-To: <1274.10.1.1.39.1026335078.squirrel@www.la-rubber.com> Cc: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Message-Id: <200207110838.28005.barwick@gmx.net> X-Archive-Number: 200207/153 X-Sequence-Number: 8532 On Wednesday 10 July 2002 23:04, David Durst wrote: > I am not sure what the end select does, so if you can give me a explinati= on > it would be apreciated It is there to satisfy the requirement that the function should return a value from a select statement: "12.2. Query Language (SQL) Functions SQL functions execute an arbitrary list of SQL statements, returning the result of the last query in the list, which must be a SELECT. ..." (see http://www.postgresql.org/idocs/index.php?xfunc-sql.html ) After an INSERT statement you need to provide an arbitrary select statement, which could return anything. All the example does is return a string telling you what you've just done. Ian Barwick > > On Wednesday 10 July 2002 21:59, David Durst wrote: > >> Is there anyway to create a insert function? > >> I am trying: > >> CREATE FUNCTION > >> add_user(varchar(20),varchar(20),varchar(20),int4,int4) RETURNS int4 > >> AS 'INSERT INTO usr > >> (user_name,first_name,last_name,permission_set_id,customer_id) values > >> ($1,$2,$3,$4,$5)' language 'sql'; > >> > >> and get: > >> > >> ERROR: function declared to return integer, but final statement is > >> not a SELECT > >> I thought that a insert would return a internal row #, but I am not > >> sure about this. > > > > You can do something like this: > > > > CREATE FUNCTION add_user(varchar(20),varchar(20),varchar(20),int4,int4) > > RETURNS VARCHAR AS > > 'INSERT INTO usr > > (user_name,first_name,last_name,permission_set_id,customer_id) > > VALUES > > ($1,$2,$3,$4,$5); > > SELECT ''created user ''|| $1::VARCHAR' > > language 'sql'; > > > > HTH > > > > Ian Barwick > > > > > > ---------------------------(end of > > broadcast)--------------------------- TIP 5: Have you checked our > > extensive FAQ? > > > > http://www.postgresql.org/users-lounge/docs/faq.html