Stored Procedure not working in MySQL - mysql

DELIMITER $$
DROP PROCEDURE IF EXISTS `pawn`.`simpleproc`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `pawn`.`simpleproc`(OUT param1 int, inout incr int)
BEGIN
declare incr Integer;
set incr= incr+1;
SELECT count(*) into param1 FROM pawnamount;
END $$
This is my code to create a stored procedure....It's created..
For execute..
call simpleproc(#param1,#incr);
select #param1,#incr
The Result will be null values.. It is the simple one.. I've tried many times.But,I get null values only..

DECLARE incr INT; -- incr is NULL here, add DEFAULT 0 if you want it to have a value
SET incr = incr + 1 -- NULL + 1 is still NULL
SELECT COUNT(*) INTO param1 FROM pawnamount; -- If the table pawnamount is empty, it generates an empty set, which in a parameter assignment becomes NULL.

Since you define incr as an INOUT paramater, you should not declare it again in the body of your procedure. This way you can increment it properly as long as it is initialized before being passed to your procedure.
Here's the code:
DELIMITER $$
DROP PROCEDURE IF EXISTS `pawn`.`simpleproc`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `pawn`.`simpleproc`(OUT param1 int, inout incr int)
BEGIN
set incr= incr+1;
SELECT count(*) into param1 FROM pawnamount;
END $$
DELIMITER ;
set #incr = 0;
call simpleproc(#param1,#incr);
select #param1,#incr;

Related

MySQL returns value, but variable is null

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

MySQL procedure with multi in parameters when you can pass only one

I have create MySQL procedure having multiple IN parameters. I want to call procedure with few parameters but when I leave other fields blank it shows this error:
DELIMITER $$
CREATE DEFINER=itzakeed_akeed#localhost PROCEDURE ApiKez(
IN Choice VARCHAR(100),
IN ValidKey VARCHAR(100),
IN azid INT(5),
IN amts FLOAT(50)
)
BEGIN
DECLARE GetKey VARCHAR(100);
DECLARE Balance FLOAT;
CASE WHEN Choice='KeyCheck' THEN
SELECT COUNT(id) INTO GetKey
FROM users
WHERE api_key=ValidKey;
if key is valid
IF GetKey=1 THEN
SELECT *
FROM users
WHERE key=ValidKey;
ELSE
SELECT 0;
END IF;
ELSE
SELECT "INVALID INPUT CHOICE";
END CASE;
END
$$
DELIMITER ;
No you cannot call a procedure with less parameters than what is required. However, you can pass null or empty strings and check if a parameter has a value from withing the stored procedure.

Output parameter of stored procedure not showing

I can't seem to get my stored procedure to show the OUT Parameter when it is called. It just shows '0 row affected' with no display of the OUTPUT Variable. The OUTPUT is the value of a variable that was declared. This is my procedure
CREATE DEFINER=`root`#`%` PROCEDURE `test`(IN `masterId` INT, IN `subId` INT, OUT current_balance INT)
BEGIN
declare current_balance INT;
set current_balance = 2;
END
This is how I am calling it:
call test('274', '399', #res)
You have overridden the parameter with a local variable.
I prefer to give names to parameters to avoid conflicts:
CREATE DEFINER=`root`#`%` PROCEDURE `test`(
IN in_masterId INT,
IN in_subId INT,
OUT out_current_balance INT
)
BEGIN
set out_current_balance = 2;
END;

Passing Stored Procedure Results into Parameters

I am calling a stored procedure (1) within a stored procedure (2). Is there a way to pass values of (1) INTO parameters of (2) so that I can return to the calling program? Thank you.
Yes. You can define parameters to be INOUT or OUT.
This allows the procedure to pass values back to the caller.
As a brief example, proc1 has parameters that are defined as INOUT and OUT. In proc1 values are assigned to those parameters.
And proc2 calls proc1. The values set in proc1 are available to proc2.
DELIMITER ;
CREATE PROCEDURE proc1(INOUT ua INT, OUT ob INT)
BEGIN
SET ua = ua + 1;
SET ob = 1;
END$$
CREATE PROCEDURE proc2()
BEGIN
DECLARE a INT;
DECLARE b INT;
SET a = 0;
SET b = 0;
CALL proc1(a,b);
SELECT a, b;
END$$
DELIMITER ;
CALL proc2();
a b
---- ----
1 1

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).