Cannot change OUT variable in stored procedure - mysql

I try to change OUT variable in stored procedure. Before call procedure I set my_variable to 9. I have no errors, but after CALL statement variable is NULL
CREATE PROCEDURE getAllMen(OUT my_variable int)
BEGIN
CASE my_variable
WHEN my_variable <= 10 THEN SELECT 44 INTO my_variable;
WHEN my_variable > 10 THEN SELECT 55 INTO my_variable;
ELSE BEGIN END;
END CASE;
END
SET #start_id = 9;
SELECT #start_id;
CALL getAllMen(#start_id);
Thanks.

Try:
DROP PROCEDURE IF EXISTS `getAllMen`;
CREATE PROCEDURE `getAllMen`(
/*OUT `my_variable` INT*/
INOUT `my_variable` INT
)
BEGIN
CASE/* `my_variable` */
WHEN `my_variable` <= 10 THEN
SELECT 44 INTO `my_variable`;
WHEN `my_variable` > 10 THEN
SELECT 55 INTO `my_variable`;
END CASE;
END;
SET #`start_id` := 9;
SELECT #`start_id`;
CALL `getAllMen`(#`start_id`);
SELECT #`start_id`;
See db-fiddle.
UPDATE
See documentation:
13.1.16 CREATE PROCEDURE and CREATE FUNCTION
Syntax
...
An OUT parameter passes a value from the procedure back to the
caller. Its initial value is NULL within the procedure, and its
value is visible to the caller when the procedure returns.
...

Related

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.

SQL Stored Process: If statement not behaving as expected

Attempting to create a stored process for MySQL. It contains a basic if-statement. The current script is below:
DROP PROCEDURE IF EXISTS sp_pay_raise;
DELIMITER ##
CREATE PROCEDURE sp_pay_raise
(IN inEmpId INT,
IN inPercentageRaise DOUBLE(4,2),
OUT outErrorCode INT)
BEGIN
IF (#inPercentageRaise <= 0.0) THEN
SELECT -3 INTO errorCode
ELSE
SELECT -2 INTO errorCode
END IF;
END ##
DELIMITER ;
The above doesn't work as expected. If I provide a inPercentageRaise which is less than zero, for ex.
CALL sp_pay_raise(0,-1.0, #out)
SELECT #out;
The database shows #out = -2. Is the if-statement which is written incorrectly?
# is used for Session variables. While inside a stored procedure, you don't need to use # for the input params. Otherwise, MySQL will look for a similar name pre-defined Session variable. Since it does not find it; it assumes its values as Null
There are other errors also. You have a typo; instead of using outErrorCode param, you are using a different undefined variable errorCode
DROP PROCEDURE IF EXISTS sp_pay_raise;
DELIMITER ##
CREATE PROCEDURE sp_pay_raise
(IN inEmpId INT,
IN inPercentageRaise DOUBLE(4,2),
OUT outErrorCode INT)
BEGIN
IF (inPercentageRaise <= 0.0) THEN -- Remove # from here.
SELECT -3 INTO outErrorCode -- It should be outErrorCode instead of errorCode
ELSE
SELECT -2 INTO outErrorCode -- It should be outErrorCode instead of errorCode
END IF;
END ##
DELIMITER ;

Wrong result while declaring a variable inside Stored procedure

Following is a simple stored procedure to calculate male count from a table , I have declared a variable total_count inside the proc where i'm storing my result.
DELIMITER //
CREATE PROCEDURE GetMaleCount()
BEGIN
DECLARE total_count INT DEFAULT 0 ;
SELECT COUNT(STUDENT_ID) INTO total_count
FROM [table1]
where STUDENT_GENDER = 'M' ;
END //
DELIMITER ;
call GetMaleCount();
select #total_count as tc;
When i executed this procedure i'm getting NULL as the answer, but when i seperately executed just the inner sql query i got the right answer 1852. have i declared the variable in the wrong way ?
total_count that you've declared is visible only in procedure. That is why it is NULL outside of it. You need to use OUT parameter when defining procedure:
DELIMITER //
CREATE PROCEDURE GetMaleCount(OUT total_count INT)
BEGIN
SELECT COUNT(STUDENT_ID) INTO total_count
FROM [table1]
where STUDENT_GENDER = 'M' ;
END //
DELIMITER ;
call GetMaleCount(#total_count);
select #total_count as tc;
You need to use OUT parameter.
DELIMITER //
CREATE PROCEDURE GetMaleCount(OUT total_count INT)
BEGIN
SELECT COUNT(STUDENT_ID) INTO total_count
FROM [table1]
where STUDENT_GENDER = 'M' ;
END //
DELIMITER ;
call GetMaleCount(#total_count);
select #total_count as tc;

Incorrect number of arguments for PROCEDURE

Define procedure:
DELIMITER $$
CREATE PROCEDURE SP_Reporting(IN tablename VARCHAR(20))
proc_label:BEGIN
IF tablename IS NULL THEN
SELECT 'Null detect';
LEAVE proc_label;
END IF;
SELECT 'after';
END;
$$
DELIMITER ;
Call Procedure:
CALL SP_Reporting();
Error :
ERROR 1318 (42000): Incorrect number of arguments for PROCEDURE
cds.SP_Reporting ; expected 1, got 0
How pass var by default like SP_Reporting(IN tablename = 'default value' VARCHAR(20))
When you are making your stored procedure, you can assign a value to your input, so there is no need to pass parameters while you are calling the proc.
We usually assign NULL and for making parameters optional, we use this method.
tablename VARCHAR(20) = NULL
Your complete script:
DELIMITER $$
CREATE PROCEDURE SP_Reporting(IN tablename VARCHAR(20) = NULL)
proc_label:BEGIN
IF tablename IS NULL THEN
SELECT 'Null detect';
LEAVE proc_label;
END IF;
SELECT 'after';
END;
$$
DELIMITER ;
EDIT
MySQL is not accepting optional parameters. So one way is to pass NULL value in your stored procedure and check it with IF statement inside your proc.
DELIMITER $$
CREATE PROCEDURE SP_Reporting(IN tablename VARCHAR(20))
proc_label:BEGIN
IF tablename IS NULL THEN
-- Do something;
ELSE
-- Do something else;
END IF;
END;
$$
DELIMITER ;
You have to pass the table name in the procedure call statement like:
CALL SP_Reporting(table_name);
you can't pass default in call statement.
You can assign default value before calling the procedure.
or use OUT instead of IN as a parameter.
you miss the parameter :tablename
you should like this :
call SP_Reporting('any varchar')
or
call SP_Reporting(null)

Mysql stored procedure multiple selects

I am running a stored procedure. The issue seems to be that it will go into the if statement. Also for some reason or another regardless of how many selects I use it will only return the first. I've copied this from another stored procedure that works like a charm, but this one just won't go. Any ideas?
DROP PROCEDURE IF EXISTS genSelPriceTier;
DELIMITER $$
CREATE PROCEDURE genSelPriceTier(tier_id INT, default_id INT)
BEGIN
DECLARE rowCount INT DEFAULT 0;
SELECT * FROM price_tier WHERE price_tier_id = tier_id;
SET rowCount = FOUND_ROWS();
IF rowCount < 1 THEN
SELECT * FROM price_tier WHERE price_tier_id = default_id;
END IF;
END$$
DELIMITER ;
There is a bug reported related to the usage of FOUND_ROWS(). So, I recommend using Count(*) for the number of rows returned. Something like the following should work.
DROP PROCEDURE IF EXISTS genSelPriceTier;
DELIMITER $$
CREATE PROCEDURE genSelPriceTier(tier_id INT, default_id INT)
BEGIN
DECLARE rowCount INT DEFAULT 0;
SELECT COUNT(*) INTO rowCount FROM price_tier WHERE price_tier_id = tier_id
IF rowCount < 1 THEN
SELECT * FROM price_tier WHERE price_tier_id = default_id;
END IF;
END$$
DELIMITER ;