[SQL] simple "select / if found" isn't - Mailing list pgsql-sql

From Gary Stainburn
Subject [SQL] simple "select / if found" isn't
Date
Msg-id 201612161102.37559.gary.stainburn@ringways.co.uk
Whole thread Raw
Responses Re: [SQL] simple "select / if found" isn't
List pgsql-sql
I'm creating a simple function that must have been done millions of times 
before, but I can't get it to work.  In this case, I'm checking a user ID and 
password against previously used passwords:

All I want to do is return 'found' based on the select but I can't get it to 
work.  

If I run 

select 1 from user_previous_passwords where u_id=25 and crypt('MyPaSSword',u_previous_password) = u_previous_password;

then it returns the matching row(s)

If I run my function

create or replace function check_previous_passwords (ID int4, PASS varchar) 
returns boolean as $$
DECLARE UID int4;
BEGIN return exists(select 1 from user_previous_passwords    where u_id=ID and crypt(PASS,u_previous_password) =
PASS);
END;
$$ LANGUAGE plpgsql;

I always get false;

I've tried things like 

if exist(....) then ....
select 1 into UID
select count(u_id) into UID

update .....set u_id=u_id ......
if found then

but I never get the correct result, so I think I must me doing something much 
more fundamentally wrong.

Can someone spot it please?

Gary



pgsql-sql by date:

Previous
From: Stephen Frost
Date:
Subject: Re: RLS for superuser
Next
From: Karsten Hilbert
Date:
Subject: Re: [SQL] simple "select / if found" isn't