How can I do "if exists" in pgplsql?

Started by adbabout 25 years ago2 messagesgeneral
Jump to latest
#1adb
adb@Beast.COM

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.

#2Jan Wieck
JanWieck@Yahoo.com
In reply to: adb (#1)
Re: How can I do "if exists" in pgplsql?

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