Обсуждение: Re: [SQL] simple "select / if found" isn't

Поиск
Список
Период
Сортировка

Re: [SQL] simple "select / if found" isn't

От
Jan Otto
Дата:
hi gary,

> On 16 Dec 2016, at 12:02, Gary Stainburn <gary.stainburn@ringways.co.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


Re: [SQL] simple "select / if found" isn't

От
Gary Stainburn
Дата:
On Friday 16 December 2016 11:34:48 Jan Otto wrote:
>
> 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

What a muppet am I???????

It had to be something that simple, but I was going code blind.  The ironic 
bit was that the where clause was the only thing that remained the same with 
ever different idea I tried.

Thanks Jan