Thread: How can I do "if exists" in pgplsql?

How can I do "if exists" in pgplsql?

From
adb
Date:
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

Thanks,

Alex.


Re: How can I do "if exists" in pgplsql?

From
Jan Wieck
Date:
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