Why doesn't this MySQL function work with VARCHAR parameters? - mysql

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 ;

Related

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

Return result set in OUT parameter of Stored Procedure in MySQL

I have procedure in MySQL which has two IN parameters: userLogin(VARCHAR) and userPassword(VARCHAR), and two OUT parameters: userID (INT) and userRights(VARCHAR).
The procedure works as follows: check, if user with given login and password is in the database, if so, return his ID, userRights and settings. Rights can be concatenated into a string, but i would like to avoid concatenating and subsequently parsing websettings, since the structure of the data is more complicated.
Now, my procedure returns only userID and userRights and if succesfull, I run another select statement to get the settings.
Current procedure:
DELIMITER $$
CREATE PROCEDURE `proc_login_user`(IN UserName VARCHAR(45), IN Pwd VARCHAR(250), OUT Uid INT, OUT Rights VARCHAR(100))
BEGIN
DECLARE Uname VARCHAR(45);
DECLARE Pass VARCHAR(250);
DECLARE UserId INT;
SET #Uname := UserName;
SET #Pass := Pwd;
SET #UserId = (SELECT ID FROM `users` WHERE Login = #Uname AND Password = #Pass);
SET Uid = #UserId;
SET Rights = /* some SELECT GROUP_CONCAT statement to create string */
END;
I wish to add one more output parameter to procedure: userSettings, which should be a result set.
Something like this:
DELIMITER $$
CREATE PROCEDURE `proc_login_user`(IN UserName VARCHAR(45), IN Pwd VARCHAR(250), OUT Uid INT, OUT Rights VARCHAR(100), OUT Settings)
BEGIN
DECLARE Uname VARCHAR(45);
DECLARE Pass VARCHAR(250);
DECLARE UserId INT;
SET #Uname := UserName;
SET #Pass := Pwd;
SET #UserId = (SELECT ID FROM `users` WHERE Login = #Uname AND Password = #Pass);
SET Uid = #UserId;
SET Rights = /* some SELECT GROUP_CONCAT statement to create string */
SET Settings = SELECT * FROM `settings` WHERE UserID = #UserId;
END;
Any help would be appreciated.
Thanks,
Zbynek
MySQL does not permit variables to contain a result set or table; they are single scalar values only.
Within a stored procedure, you can simply run a SELECT, and the result set will be returned to the client. You can't use that result set within other stored routines inside MySQL, but an application that called the procedure could consume it just like any other query result.
As an aside:
DECLARE Uname VARCHAR(45);
SET #Uname := UserName;
These two lines have nothing to do with each other. The local variable 'Uname' and the user variable '#Uname' are completely separate things. In this particular case, you don't need either one. You can refer to IN/OUT variables directly in queries (just make sure those variables are not the same as the name of a column in the table, or you may get unexpected results.)
DELIMITER $$
CREATE PROCEDURE `proc_login_user`(IN UserName VARCHAR(45), IN Pwd VARCHAR(250), OUT Uid INT, OUT Rights VARCHAR(100))
BEGIN
SELECT ID INTO Uid FROM `users` WHERE Login = UserName AND Password = Pwd;
/* some SELECT GROUP_CONCAT INTO Rights statement to create string */
SELECT * FROM `settings` WHERE UserID = Uid;
END;

passing the value if exists to another insert query MySQL

I have the following query running against mysql:
DELIMITER $$
CREATE
/*[DEFINER = { user | CURRENT_USER }]*/
PROCEDURE `mybook_store`.`myProc`()
/*LANGUAGE SQL
| [NOT] DETERMINISTIC
| { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }
| SQL SECURITY { DEFINER | INVOKER }
| COMMENT 'string'*/
BEGIN
DECLARE authId INT DEFAULT 0;
DECLARE
DECLARE CheckExists INT DEFAULT 0;
SET CheckExists = 0;
SELECT COUNT(*) INTO CheckExists FROM author WHERE fname = 'xxxx' AND surname='xxxx';
IF (CheckExists > 0) THEN
SELECT authorid INTO authId FROM author WHERE fname = 'xxxx' AND surname='xxxx';
ELSE
INSERT author(fname, surname) VALUES('xxxx', 'xxxx');
SET authId = LAST_INSERT_ID();
END IF;
END$$
DELIMITER ;
the above query will check if the inserted author exists in author table if not it will insert it if exist will not insert...
my questions:
i want to to get fname and surname if exists to variables then
insert in another query which insert into another table requires the
2 variables how to pass it to the query?
shall i use the authorid if exists then select fname and surname
that belong to authorid?
can i declare parameters instead of the actual value 'xxxx'?
thank you for your help
You're close, try this:
DELIMITER $$
CREATE PROCEDURE myProc (IN f_fname VARCHAR(25), IN f_surname VARCHAR(25))
BEGIN
DECLARE checkExists INT DEFAULT 0;
DECLARE authId INT DEFAULT 0;
SELECT COUNT(*) INTO checkExists FROM author WHERE fname = f_fname AND surname = f_surname;
IF(checkExists > 0) THEN
SELECT authorId into authId FROm author WHERE fname = f_fname AND surname = f_surname;
ELSE
INSERT INTO author(fname, surname) VALUES (f_fname, f_surname);
SET authId = LAST_INSERT_ID();
END IF;
END$$
DELIMITER ;

mysql stored procedure login

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.

How can I get and assign the return value from MySQL stored procedure

I have the next stored procedure:
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `getUserIdByLogin`(userId VARCHAR(255))
BEGIN
SELECT id FROM `userdata` WHERE login = userId;
END
I want to declare a new variable #tmp for e.g. and do smth to this:
SET #tmpValue = CALL getUserIdByLogin("someLogin");
But it doesn't work.
If just to call:
CALL getUserIdByLogin("someLogin");
Then I would see results, but I need to declare the results in the variable ( of array type ).
How can I do it?
Thanks!
CREATE DEFINER=`root`#`localhost` PROCEDURE `getUserIdByLogin`(
userId VARCHAR(255),
OUT idout int
)
BEGIN
SELECT id INTO idout FROM `userdata` WHERE login = userId;
END
then
SET #id = 0;
CALL getUserIdByLogin("someLogin", #id);
SELECT #id;