pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Josh Berkus <josh@agliodbs.com>
To: Lonnie Cumberland <lonnie_cumberland@yahoo.com>
To: Josh Berkus <josh@agliodbs.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: Calling plSQL functions
Date: Thu, 12 Apr 2001 08:38:48 -0700
Message-ID: <web-37277@davinci.ethosmedia.com> (raw)
In-Reply-To: <20010412132354.46676.qmail@web12506.mail.yahoo.com>

Lonnie,

> Sorry for the bad terminology as I will try to get it corrected as I
> have a
> better learning of how to use postgresql properly.

No problem.  I just wanted to clarify your question; I didn't understand
it.  

> create function register_user(text,text,text,text,text,text,text,text,text)
> returns text as '

First off, I believe that you will see some performance improvement if
you use VARCHAR instead of TEXT (except, of course, for very long (> 500
chars) strings).  Also, not all RDBMS's support the TEXT type, but all
do support VARCHAR.  FYI.


> declare
>  
>     client_title        ALIAS FOR $1;
>     first_name          ALIAS FOR $2;
>     middle_name         ALIAS FOR $3;
>     last_name           ALIAS FOR $4;
>     email_address       ALIAS FOR $5;
>     company_name        ALIAS FOR $6;
>     client_login        ALIAS FOR $7;
>     client_passwd       ALIAS FOR $8;
>     client_passwd2      ALIAS FOR $9;
>  
>     retval              text;
>  
> begin
>  
>     -- Look to see if the login is already taken
>     select * from user_info where login = client_login;
>  
>     -- If YES then return the error
>     if found then
>         return ''LoginExists'';
>     end if;

This is your problem, right here.  The PL/pgSQL handler interprets an
un-intercepted SELECT as an attempt to return a rowset from the
function.  Returning rowsets is entirely the province of stored
procedures (not yet supported under postgres) and thus the function
handler errors out.

What you really want is:

> 	login_check INT4;
> begin
>  
>     -- Look to see if the login is already taken
>     SELECT id INTO login_check
>     FROM user_info where login = client_login;
>  
>     -- If YES then return the error
>     if login_check > 0 then
>         return ''LoginExists'';
>     end if;

The INTO intecepts the result of the SELECT statement and passes it off
to a variable.  This would also allow you to return something more
informative:

> 	login_check VARCHAR;
> begin
>  
>     -- Look to see if the login is already taken
>     SELECT first_name || '' '' || last_name INTO login_check
>     FROM user_info where login = client_login;
>  
>     -- If YES then return the error
>     if login_check <> '''' then
>         return ''That login already exists for user '' || login_check
|| ''.  Please choose another.'';
>     end if;

(assuming that first_name and last_name are required and thus NOT NULL).

Hope that helps.

-Josh Berkus



______AGLIO DATABASE SOLUTIONS___________________________
                                       Josh Berkus
  Complete information technology      josh@agliodbs.com
   and data management solutions       (415) 565-7293
  for law firms, small businesses        fax 621-2533
    and non-profit organizations.      San Francisco



view thread (10+ messages)  latest in thread

Message-ID: <web-37277@davinci.ethosmedia.com>
Permalink:  ../web-37277@davinci.ethosmedia.com/
Also on:    postgresql.org/message-id/web-37277@davinci.ethosmedia.com

 · 

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: josh@agliodbs.com, lonnie_cumberland@yahoo.com
  Subject: Re: Calling plSQL functions
  In-Reply-To: <web-37277@davinci.ethosmedia.com>
  References: <20010412132354.46676.qmail@web12506.mail.yahoo.com> <web-37277@davinci.ethosmedia.com>

* 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