Re: How can I do "if exists" in pgplsql? - Mailing list pgsql-general

From Jan Wieck
Subject Re: How can I do "if exists" in pgplsql?
Date
Msg-id 200101291923.OAA04465@jupiter.greatbridge.com
Whole thread Raw
In response to How can I do "if exists" in pgplsql?  (adb <adb@Beast.COM>)
List pgsql-general
adb wrote:
> Is there a way for me inside a function
> to do some conditional logic based on the existence of a row
> like
>
> if exists select userId from users where usrId = $1
>    do something
> else
>    do something else

    What about

          CREATE FUNCTION f3(name) RETURNS bool AS '
          DECLARE
              row     record;
          BEGIN
              SELECT * INTO row FROM pg_user WHERE usename = $1;
              IF FOUND THEN
                  RAISE NOTICE ''user % exists'', $1;
                  RETURN ''t'';
              ELSE
                  RAISE NOTICE ''user % not found'', $1;
                  RETURN ''f'';
              END IF;
          END;
          ' LANGUAGE 'plpgsql';


Jan

--

#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me.                                  #
#================================================== JanWieck@Yahoo.com #



_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com


pgsql-general by date:

Previous
From: adb
Date:
Subject: How can I do "if exists" in pgplsql?
Next
From: Camm Maguire
Date:
Subject: Re: Calculated values