I have an issue where I'm trying to make a stored function to search for a user's username and password. The arguments to the function should be the username and password and it must return true if the username and password combo exist and false if it doesn't. This is what I've created so far with no success:
delimiter $$
create function cred(u varchar(15), p varchar(6))
returns char(5)
begin
declare a CHAR(5);
if (select username = u and pwd = p from customers) then set a = 'true';
else set a = 'false';
end if;
end$$
delimiter ;
select cred('dJete', 'abc112');
You need to use return-statement in function to return the value. Also, minor fixes to the actual query where you match the customer:
create function cred(in_username varchar(15), in_password varchar(6))
returns char(5)
begin
declare v_cnt int;
select count(*) into v_cnt
from customers
where username = in_username and pwd = in_password;
if (v_cnt>0) then
return 'true';
else
return 'false';
end if;
end
$$
Related
I am trying to create a function in MySQL like so, bu tI am getting a syntax error at the if exists line:
I think I am doing something slightly off as a result of translation from MS SQL server.
CREATE FUNCTION MyFunction(input_field INTEGER)
RETURNS VARCHAR(5)
BEGIN
IF EXISTS (SELECT * FROM Teaches WHERE courseid = input_field)
RETURN 'True'
RETURN 'false'
END;
**UPDATE
The solution I found based on the answer from #SK Jajoriya
DELIMITER $$
CREATE FUNCTION MyFunction2(input_field INTEGER)
RETURNS VARCHAR(5)
BEGIN
IF EXISTS (SELECT * FROM Teaches WHERE courseid = input_field) THEN
RETURN 'True';
ELSE
RETURN 'False';
END IF;
END $$
CREATE FUNCTION MyFunction(input_field INTEGER)
RETURNS VARCHAR(5)
BEGIN
IF EXISTS (SELECT * FROM Teaches WHERE courseid = input_field) THEN
RETURN 'True'
ELSE
RETURN 'false'
END IF;
END;
In your case, the if statement not closed
IF should be finished with END IF
https://dev.mysql.com/doc/refman/5.7/en/if.html
But in your case it's better to
CREATE FUNCTION MyFunction(input_field INTEGER)
RETURNS VARCHAR(5)
BEGIN
DECLARE res VARCHAR(5);
SET res = IF(EXISTS (SELECT 1 FROM Teaches WHERE courseid = input_field LIMIT 1),'true','false');
RETURN res;
END;
here is a fiddle
https://www.db-fiddle.com/f/uFkXXDpqVjn6AjYkXx3EYM/0
Why doesn't this MySQL function work with VARCHAR parameters? It works perfectly when parameter is removed
DELIMITER //
CREATE FUNCTION GETNAME(index varchar(30))
RETURNS varchar(50)
BEGIN
DECLARE fullname varchar(50);
SELECT name into fullname from roles where roles.id = (select role_id from user_role where user_role.user_id = (select id from users where users.index_nb = index)) ;
RETURN fullname;
END //
DELIMITER ;
I created a stored procedure which requirement as below
When I execute the stored procedure with one username typed in which be compare if username are exists in database then (variable) result_userId will set at userId ELSE if username dose not exists in database then (variable) result_userId will be set the number 99
BUT I CANNOT DO THAT
Please help me !
::CODE BELOW::
DELIMITER $$
USE `shoppy` $$
CREATE PROCEDURE `testProc02`
(
IN _username CHAR(50),
OUT result_userId INT UNSIGNED
)
BEGIN
SELECT #uId := userId FROM user
WHERE userName = _username;
IF #uId = NULL THEN
SET result_userId = 99;
ELSE
SET result_userId = #uId;
END IF;
END $$
DELIMITER ;
When I CALL testProc02();
enter image description here
You can't use the equality operator = on null. You have to test the expression IS NULL or use the null-safe equality operator <=>.
Your code should be
DELIMITER $$
USE `shoppy` $$
CREATE PROCEDURE `testProc02`
(
IN _username CHAR(50),
OUT result_userId INT UNSIGNED
)
BEGIN
SELECT #uId := userId FROM user
WHERE userName = _username;
IF #uId IS NULL THEN
SET result_userId = 99;
ELSE
SET result_userId = #uId;
END IF;
END $$
DELIMITER ;
What happens is that #uID = NULL always evaluates to null, which the if interprets as false.
I am using mysql and have a procedure for login authentication,
CREATE PROCEDURE CheckPassword (IN username CHAR(8),IN password_p VARCHAR(20), OUT yes_no char(1))
BEGIN
IF EXISTS(SELECT * FROM USER WHERE USER_ID = username AND password = password_p) then
set yes_no = '0';
ELSE
set yes_no = '1';
END IF;
END;
But it mysql warned that having error when creatinng the procedure, it say have the error i nfor line 4, it is the line of "set yes_no = '0';"? I have try this way too,
CREATE PROCEDURE CheckPassword (IN username CHAR(8),IN password_p VARCHAR(20), OUT yes_no char(1))
BEGIN
IF EXISTS(SELECT * FROM USER WHERE USER_ID = username AND password = password_p) then
select '0' into yes_no;
ELSE
select '1' into yes_no;
END IF;
END;
Didn't work too, is that a must to use delimeter when create procedure? and can u tell me statement of calling this procedure,
I think that the problem was that USER is a reserved word. Also, I suggest you use a function, not a procedure (you just want a value to be returned).
This function works:
DELIMITER ||
CREATE FUNCTION CheckPassword (username VARCHAR(8), password_p VARCHAR(20))
RETURNS BOOL
NOT DETERMINISTIC
READS SQL DATA
BEGIN
RETURN EXISTS (SELECT username FROM `USER` WHERE USER_ID = username AND password = password_p);
END;
||
DELIMITER ;
SELECT CheckPassword('a','b');
Try this:
CREATE PROCEDURE `IsUserPasswordValid`(
IN username varchar(50),
IN password_p VARCHAR(20),
OUT yes_no int
)
BEGIN
SELECT count(*) INTO yes_no
FROM USER u
WHERE u.USER_ID = username && u.password = password_p;
END
If it returns 1 then user is valid.
I'm trying to write a MySQL function which returns whether an username is in my table or not. It's the following:
CREATE FUNCTION UserExists (pUserName VARCHAR(40))
RETURNS BIT DETERMINISTIC
BEGIN
DECLARE rVal BIT;
IF EXISTS (SELECT * FROM Users WHERE userName = pUserName)
THEN SET rVal = 1;
ELSE SET rVal = 0;
END IF;
RETURN rVal;
END;
However, I get an error. Any ideas?
try this
IF(EXISTS(SELECT * FROM Users WHERE userName = pUserName))
how about using user define variable?
DELIMITER $$
CREATE FUNCTION UserExists (pUserName VARCHAR(40))
RETURNS BIT DETERMINISTIC
BEGIN
DECLARE rVal BIT;
SET #recCount := (SELECT COUNT(*) FROM Users WHERE userName = pUserName);
IF #recCount > 0 THEN
SET rVal = 1;
ELSE
SET rVal = 0;
END IF;
RETURN rVal;
END $$
DELIMITER ;
Before you query your mysql database, you need to use %mysql sudo -u -p and then give your commands.