Simple MySQL function error - mysql

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.

Related

Using a custom(user-defined) function inside a procedure in mysql

In the code below, there's a function that generates a random string, and a procedure that is supposed to insert random strings into a table called Users. I have successfully created the function and it's working without any problem, but when I try to create the procedure, it returns a syntax error at line "SET #str = SELECT randstring(8);" I think I am trying to call my function in a wrong way. I'm new to databases so please bear with me.
DELIMITER $$
CREATE FUNCTION `Randstring`(LENGTH SMALLINT(3)) RETURNS VARCHAR(100) CHARSET utf8
BEGIN
SET #returnStr='';
SET #allowedChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
SET #i = 0;
WHILE (#i < LENGTH) DO
SET #returnStr = CONCAT(#returnStr, SUBSTRING(#allowedChars, FLOOR(RAND() * LENGTH(#allowedChars) + 1), 1));
SET #i = #i + 1;
END WHILE;
RETURN #returnStr;
END
DELIMITER $$
CREATE PROCEDURE insertdata()
BEGIN
SET #str='';
DECLARE i INT DEFAULT 0;
WHILE i <= 1000 DO
SET #str = SELECT randstring(8);
INSERT INTO Users (user_name)
VALUES(#str);
SET i = i + 1;
END WHILE;
END $$
DELIMITER ;
Presumably, you intend either:
SET #str = randstring(8);
Or:
SELECT #str := randstring(8);
Or:
SET #str = (SELECT #randstring(8));
A SELECT when used as a subquery needs a parentheses before it. However, no subquery is really necessary.

how to tell if a mysql insert is successful using while loop?

I am inserting the records in mysql database using while loop.I want to chcek the check atleast one record is inserted or not. I tried below code but ROW_COUNT() give me success, if the record is not inserted.
DELIMITER $$
DROP PROCEDURE IF EXISTS test$$
CREATE PROCEDURE test()
BEGIN
DECLARE count INT DEFAULT 0;
DECLARE res varchar(255);
WHILE count < 10 DO
/**Sql statement**/
SET count = count + 1;
END WHILE;
IF ROW_COUNT() > 0 THEN
SET res = 'success';
ELSE
SET res = 'failure';
END IF;
SELECT res;
END$$
DELIMITER ;

Stored Functions

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
$$

MySQL query - weird syntax error

I'm using phpmyadmin and MySQL to run a simple query, that creates a function checking the existence of a certain record. It keeps throwing a syntax error at line 7 with Declare. I have no idea why. I did try to use the built-in function creator, but it's messed up and I don't like it. Any help appreciated!
CREATE FUNCTION check_if_card_exists (_name TEXT)
RETURNS INT
DETERMINISTIC
READS SQL DATA
BEGIN
DECLARE res INT; --line 7
IF EXISTS (SELECT `name` FROM `cards` WHERE `name` = _name)
THEN SET res = 1;
ELSE SET res = 0;
END IF;
RETURN res;
END
Try the following:
DELIMITER $$
DROP FUNCTION IF EXISTS `check_if_card_exists`$$
CREATE FUNCTION check_if_card_exists (_name TEXT)
RETURNS INT
DETERMINISTIC
READS SQL DATA
BEGIN
DECLARE res INT; --line 7
IF EXISTS (SELECT `name` FROM `cards` WHERE `name` = _name)
THEN SET res = 1;
ELSE SET res = 0;
END IF;
RETURN res;
END$$
DELIMITER ;

IF statement in MySQL stored function goes wrong

I get a error on line 12 (at the endif statement).
I belief I´m doing something wrong within the IF or ELSE, can anyone help me?
DELIMITER $$
CREATE FUNCTION TEST (`param` INT)
RETURNS INT
DETERMINISTIC
BEGIN
DECLARE `var` INT;
SET `var` = 1;
IF `param` > 0 THEN
SET `var` = `var` + `param`;
END IF;
RETURN `var`;
END$$
EDIT: (same function with case instead of if, same problem)
DELIMITER $$
CREATE FUNCTION TEST (`param` INT)
RETURNS INT
DETERMINISTIC
BEGIN
DECLARE `var` INT;
SET `var` = 1;
SET `var` =
CASE
WHEN `param` > 0 THEN `var` + `param` ELSE `var`
END ;
RETURN `var`;
END$$
Try this instead:
BEGIN
DECLARE `var` INT;
SET var =
CASE
WHEN param > 0 THEN var + 1 ELSE var
END ;
RETURN var;
END$$
Found out it was a bug in PHPMyAdmin, if I added the function with 'add routine' it worked!