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;
Related
I make a this stored function to return the amount of invoices per customer:
delimiter //
CREATE FUNCTION function1(id INT) RETURNS INT READS SQL DATA
BEGIN
DECLARE result INT;
(SELECT count(invoice_id) INTO #result FROM invoices WHERE customer_id = #id);
RETURN #result;
END//
delimiter ;
but when I use it, returns 0:
SELECT function1(12) AS Q;
and the query returns 428 :
SELECT count(invoice_id) AS Q FROM invoices WHERE customer_id = 12;
I need to know what am I doing wrong.
#id is not the same as id
But it id better to use variable names _id to differentiate variables from column names
delimiter //
CREATE FUNCTION function1(_id INT) RETURNS INT READS SQL DATA
BEGIN
DECLARE result INT;
(SELECT count(invoice_id) INTO #result FROM invoices WHERE customer_id = _id);
RETURN #result;
END//
delimiter ;
trying to get the total number of boxes ordered from a given type("mytype").
something seems to be wrong with my syntax
create function NumOrdersForBoxType (mytype varchar(20))
returns int
begin
DECLARE #numorders int
select #numOrders = count(*)
from BOXES as B
where B.Type = mytype
return #numOrders
end
;
In mysql you don't declare user defined variables (at variables) stackoverflow.com/questions/11754781/… AND every statement needs to be terminated and since you have more than 1 statement in the function you need to wrap in begin..end and possibly set delimiters. AND you need to SET variables
delimiter $$
create function f(mytype varchar(20))
returns int
begin
DECLARE numorders int;
set numorders = (select count(*)
from boxes as B
where B.Type = mytype
);
return numOrders;
end $$
delimiter ;
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.
...
Directions
Write a proceedure called GetAllCustomerLevelsthat returns the customerNumber, customerName, and cardtype for all customers.
Hint: Use CASE in your select statement. See: Case in Select Statement at stackoverflow or MySQL CASE operator from w3resource. This could also be done using a nested IF. See: Inline IF and CASE statements in MySQL from timmurphy.
Below is an example proceedure that returns the customerNumber and customerName for all customers:
DROP PROCEDURE IF EXISTS GetAllCustomerLevels;
DELIMITER $$
CREATE PROCEDURE GetAllCustomerLevels()
BEGIN
select customerNumber, customerName
from customers;
END$$
DELIMITER ;
Save the sql that created the GetAllCustomerLevels procedure as GetAllCustomerLevels.sql
One of my Attempts
DROP PROCEDURE IF EXISTS GetAllCustomerLevels2;
DELIMITER $$
CREATE PROCEDURE GetAllCustomerLevels2()
BEGIN
DECLARE creditlimit double;
call GetCustomerLevel(#customerNumber,#cardType);
select customerNumber, customerName, #cardType
from customers
end;
SET #cardType =
(CASE
WHEN (creditlimit > 50000) THEN 'PLATINUM'
WHEN (creditlimit <= 50000 AND creditlimit >= 10000) THEN 'GOLD'
WHEN (creditlimit < 10000) THEN 'SILVER'
ELSE 'No card'
END
);
END$$
DELIMITER ;
My result returns customerName and customerNumber but #cardType stays empty.
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 ;