R
R
Respect and good to all2018-05-06 12:28:31
PostgreSQL
Respect and good to all, 2018-05-06 12:28:31

How to write a stored function in PostgreSQL to check if the Login/Password pair matches?

Good day!
There is a long PostgreSQL table - sp_users
it has fields: sp_login and sp_pass how to write a stored function like this
using PL/pgSQL or SQL : CREATE FUNCTION AS 'function body' LANGUAGE plpgsql; And returning the result 1=is_matched_pair; 2=no_match_pair Many thanks in advance! Ps I am very beginner, if possible with comments...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
0
0xD34F, 2018-05-06
@0xD34F

CREATE FUNCTION check_login_and_password(
  _login character varying,
  _password character varying
)
  RETURNS boolean AS
$$
  SELECT EXISTS(SELECT * FROM table_name WHERE login = _login OR password = _password);
$$
LANGUAGE sql;

Well, or if you really need numbers as a result:
CREATE FUNCTION check_login_and_password(
  _login character varying,
  _password character varying
)
  RETURNS bigint AS
$$
  SELECT count(*) FROM table_name WHERE login = _login OR password = _password;
$$
LANGUAGE sql;

R
Respect and good to all, 2018-05-07
@Link-Line

Decided!
The following construction helped:
CREATE FUNCTION check_login_and_password(input_login CHARACTER VARYING, input_pass CHARACTER VARYING) RETURNS boolean AS
$$
SELECT EXISTS(SELECT * FROM sp_users WHERE "public".sp_users.p_login = input_login AND "public".sp_users.p_pass = input_pass );
$$
LANGUAGE sql;
In the request we pass:
'Login', 'Password'
If the answer is correct, it returns: t (true), if it is not true: f (false).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question