MySQL returns value, but variable is null - mysql

I have this stored procedure:
DROP PROCEDURE IF EXISTS rankvolume $$
CREATE PROCEDURE `rankvolume`(IN theranking INT, OUT thectr INT)
BEGIN
SELECT organic from ctrs where ranking = theranking;
END $$
DELIMITER;
I call it like this:
CALL rankvolume(2,#foo)
It returns .125, which is correct. YAY!
But
SELECT #foo
Returns NULL.
Booo.
I've tried renaming all variables. I double-checked that I'm not reusing any variable names.
What am I missing?

you need to add a value to thectr
DELIMITER $$
DROP PROCEDURE IF EXISTS rankvolume $$
CREATE PROCEDURE `rankvolume`(IN theranking INT, OUT thectr INT)
BEGIN
SELECT organic INTO thectr from ctrs where ranking = theranking;
END $$
DELIMITER ;
db<>fiddle here

Related

how to pass parameter to stored procedure using mysql

Im trying to add parameter to this stored procedure
DELIMITER $$
DROP PROCEDURE IF EXISTS `mifostenant-default`.`test` $$
CREATE PROCEDURE `mifostenant-default`.`test` ()
BEGIN
select * from employees
END $$
DELIMITER ;
Here is the way to make a procedure and pass a parameter to it:
delimiter $$
DROP PROCEDURE IF EXISTS `mifostenant-default`.`test`;
CREATE PROCEDURE `mifostenant-default`.`test` (IN empName CHAR(20))
BEGIN
SELECT * FROM employees WHERE employee_name=empName;
END $$
delimiter ;
And you call your procedure as below:
CALL mifostenant-default.test(the_parameter_to_pass);

mysql stored procedure error: missing semicolon

I have following store procedure. It is give me some error
DROP procedure IF exists getQueueMessage;
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `getQueueMessage`(msg varchar(100))
BEGIN
SELECT `Name` FROM queues WHERE Id IN (
SELECT PhysicalQueueId FROM indexqueuemaps WHERE ConditionFieldValue = msg)
END
END$$
DELIMITER ;
It is giving me missing semicolon error. Don't know Why this error is getting. Can someone help me?
Try like this:
DROP procedure IF exists getQueueMessage;
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `getQueueMessage`(msg varchar(100))
BEGIN
SELECT `Name` FROM queues WHERE Id IN (
SELECT PhysicalQueueId FROM indexqueuemaps WHERE ConditionFieldValue = msg);
END$$
DELIMITER ;
There's only one BEGIN and two ENDs, remove the 2nd END and you should be fine.
Replace root#localhost with root#localhost

IN() not working in mysql stored procedure

DELIMITER $$
USE `employee`$$
DROP PROCEDURE IF EXISTS `selectemployeebyids`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `selectemployeebyids`(IN empid VARCHAR(100))
BEGIN
SELECT empname,salary,department from emp where empid IN (empid) ORDER BY empid;
END$$
DELIMITER ;
can anyone please help me
With Regards
Manju K
After trying lot i find instead of IN(), FIND_IN_SET() works for me.
DELIMITER $$
USE `employee`$$
DROP PROCEDURE IF EXISTS `selectemployeebyids`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `selectemployeebyids`(IN empid VARCHAR(100))
BEGIN
SELECT empname,salary,department from emp where empid FIND_IN_SET(empid) ORDER BY empid;
END$$
DELIMITER ;
Try this
DELIMITER $$
USE `employee`$$
DROP PROCEDURE IF EXISTS `selectemployeebyids`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `selectemployeebyids`(IN empid_in VARCHAR(100))
BEGIN
SELECT empname,salary,department from emp
where concat(',',empid_in,',') like concat('%,'cast (empid as varchar(10)),',%')
ORDER BY empid;
END$$
DELIMITER ;

Help with MySQL incorrect number of arguments function error

I am getting an error in MySQL that is driving me crazy and I just can't figure out what's wrong. I makes the following call:
CALL ProfileUpdateProgress(107)
MySQL returns the error: "Incorrect number of arguments for FUNCTION ccms.fnGetProfileAlbumsPhotoCount; expected 2, got 1"
Now, as you can see in the code below, the call being made to that function is: fnGetProfileAlbumsPhotoCount(_profileId, profileUserId)
That's two arguments isn't it?? Why is it erroring??
I'm going mad!!
Database procs:
DELIMITER $$
DROP PROCEDURE IF EXISTS `ProfileUpdateProgress` $$
CREATE DEFINER=`root`#`%` PROCEDURE `ProfileUpdateProgress`(
IN _profileId integer
)
BEGIN
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
CALL ProfileUpdateProfileProgress(_profileId);
END $$
DELIMITER ;
which in turn calls:
DELIMITER $$
DROP PROCEDURE IF EXISTS `ProfileUpdateProfileProgress` $$
CREATE DEFINER=`root`#`%` PROCEDURE `ProfileUpdateProfileProgress`(IN _profileId int)
BEGIN
-- Declarations here
SELECT profileEyes, profileSex, profileHair, profileBustBand, profileBustCup, profileBirthCountry, profileProfession , profileAbout,
profileBiography, fnGetProfilePhoto(_profileId, null) AS profilePhoto, fnGetProfileAlbumsPhotoCount(_profileId, profileUserId) AS albumPhotoCount,
userAllowMultipleProfiles, profileIsPrimary, fnUserGetChildrenProfileCount(userId) AS ownerProfileCount
INTO _profileEyes, _profileSex, _profileHair, _profileBustBand, _profileBustCup, _profileBirthCountry, _profileProfession,
_profileAbout, _profileBiography, _profilePhoto, _albumPhotoCount, _userAllowMultipleProfiles, _profileIsPrimary,
_ownerProfileCount
FROM profile
INNER JOIN user
ON profileUserId = userId
WHERE profileId = _profileId;
-- Other irrelevant code here
END $$
DELIMITER ;
and the function being called that errors looks like:
DELIMITER $$
DROP FUNCTION IF EXISTS `fnGetProfileAlbumsPhotoCount` $$
CREATE DEFINER=`root`#`%` FUNCTION `fnGetProfileAlbumsPhotoCount`(
_profileId int,
_userId int
) RETURNS int(11)
BEGIN
DECLARE outProfileAlbumsPhotoCount int DEFAULT 0;
-- Irrelvant Code
RETURN outProfileAlbumsPhotoCount;
END $$
DELIMITER ;
Ah finally solved it. Another function called fnUserGetChildrenProfileCount in the select columns was the culprit as it too had a call to the fnGetProfileAlbumsPhotoCount() function and THAT call only had one argument, i.e. missing the second one.

Getting errors when trying to create a PROCEDURE in mysql

I am trying to create a mysql stored procedure, but I get this error:
Script line: 2 Failed to CREATE PROCEDURE proc_test_bideep
The stored procedure code is:
DELIMITER $$
DROP PROCEDURE IF EXISTS `commun`.`insert_categorie` $$
CREATE PROCEDURE `commun`.`insert_categorie` (id_mere INT,
lib_categ VARCHAR(50),
id_categ_sup INT ,
categ_authInstantBuy INT)
BEGIN
SET #bg_mere := (SELECT categ_bg FROM categ_basic WHERE categ_id = id_mere);
#bg_mere+2,categ_level_bideep,categ_statut,categ_adult,categ_authSmallBid,categ_authBid,categ_authInstantBuy);
SELECT '1' AS code_retour; END IF;
ecetera.........
END $$
DELIMITER ;
a) You need to DECLARE any variables on the first lines of the procedure, including their datatype:
DECLARE bg_mere INT;
b) To fetch a value from the database into a variable, you use SELECT ... INTO syntax:
SELECT categ_bg INTO bg_mere FROM categ_basic WHERE categ_basic.categ_id = id_mere;
c) You have an END IF without the corresponding IF.
d) The closing END needs a semicolon (not BEGIN though), only then do you need a delimiter to finish the entire statement, and finally you should reset the delimiter back to normal:
BEGIN
# body of the stored procedure goes here
END;
$$
DELIMITER ;
Your parameters are missing the keyword IN such as: ...(IN id_mere INT, IN lib_categ ...). Also, you need to configure your OUT variable for #bg_mere in the initial parameter list such as (IN xxx, ..., OUT bg_mere VARCHAR/INT/WHATEVER).