Re: simple "select / if found" isn't

From: Jan Otto <asche(at)me(dot)com>
To: Gary Stainburn <gary(dot)stainburn(at)ringways(dot)co(dot)uk>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: simple "select / if found" isn't
Date: 2016-12-16 11:34:48
Message-ID: C9BFAD75-EB0B-4107-A6D9-31ED34D7AF6D@me.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

hi gary,

> On 16 Dec 2016, at 12:02, Gary Stainburn <gary(dot)stainburn(at)ringways(dot)co(dot)uk> wrote:
>
> 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);

return exists(select 1 from user_previous_passwords
where u_id=ID and crypt(PASS,u_previous_password) = u_previous_password);

> END;
> $$ LANGUAGE plpgsql;

regards, jan

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Gary Stainburn 2016-12-16 11:41:41 Re: simple "select / if found" isn't
Previous Message Karsten Hilbert 2016-12-16 11:29:09 Re: simple "select / if found" isn't