Condition failing for one of the clauses in a 'case' statement - mysql

DELIMITER $$
USE `mg_ims`$$
DROP PROCEDURE IF EXISTS `sp_get_drop_down_data`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `sp_get_drop_down_data`(
IN _name VARCHAR(15))
BEGIN
CASE
WHEN _name = 'CUSTOMERS' THEN
SELECT cust_id,cust_name,address FROM tb_customers;
WHEN _name = 'SUPPLIERS' THEN
SELECT supp_name FROM tb_suppliers;
WHEN _name = 'BRANDS' THEN
SELECT b.name FROM tb_brands b;
WHEN _name = 'REGIONAL_OFFICES' THEN
SELECT r.name FROM tb_area_offices r;
WHEN _name = 'SUB_OFFICES' THEN
SELECT s.name FROM tb_locations s;
ELSE
SELECT 404 `Code`,'Case not Found' Description;
END CASE;
END$$
DELIMITER ;
All the above cases are working properly except 'regional_offices'... Although the syntax and logic is correct...

Your problem is that _name is a varchar(15), but the string 'REGIONAL_OFFICES' is 16 characters long!

Related

stored function returns 0 mysql

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 ;

how to create a function in mySQL workbench

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 ;

Creating a function in mysql with if exists

I am trying to create a function in MySQL like so, bu tI am getting a syntax error at the if exists line:
I think I am doing something slightly off as a result of translation from MS SQL server.
CREATE FUNCTION MyFunction(input_field INTEGER)
RETURNS VARCHAR(5)
BEGIN
IF EXISTS (SELECT * FROM Teaches WHERE courseid = input_field)
RETURN 'True'
RETURN 'false'
END;
**UPDATE
The solution I found based on the answer from #SK Jajoriya
DELIMITER $$
CREATE FUNCTION MyFunction2(input_field INTEGER)
RETURNS VARCHAR(5)
BEGIN
IF EXISTS (SELECT * FROM Teaches WHERE courseid = input_field) THEN
RETURN 'True';
ELSE
RETURN 'False';
END IF;
END $$
CREATE FUNCTION MyFunction(input_field INTEGER)
RETURNS VARCHAR(5)
BEGIN
IF EXISTS (SELECT * FROM Teaches WHERE courseid = input_field) THEN
RETURN 'True'
ELSE
RETURN 'false'
END IF;
END;
In your case, the if statement not closed
IF should be finished with END IF
https://dev.mysql.com/doc/refman/5.7/en/if.html
But in your case it's better to
CREATE FUNCTION MyFunction(input_field INTEGER)
RETURNS VARCHAR(5)
BEGIN
DECLARE res VARCHAR(5);
SET res = IF(EXISTS (SELECT 1 FROM Teaches WHERE courseid = input_field LIMIT 1),'true','false');
RETURN res;
END;
here is a fiddle
https://www.db-fiddle.com/f/uFkXXDpqVjn6AjYkXx3EYM/0

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;

Error in case in mysql for creating procedure

create procedure temp (in empId int)
begin
declare emptype varchar;
select emptype = qoute(emptype) from dms_document where id = empid;
select emptype
case
when emptype = 'P' then
select doctype from dms_report where pilot = 1
else
select 'No Documents required'
end case
end;
This is my query i am creating procedure in MySQL, i am getting error in case statement please hlep me why this error is coming how declare case statement why error is coming in workbench for creating procdure
You are missed comma.
CREATE PROCEDURE temp (IN empId INT)
BEGIN
DECLARE emptype VARCHAR;
SELECT emptype = qoute(emptype) FROM dms_document WHERE id = empid;
SELECT emptype,
CASE
WHEN emptype = 'P' THEN doctype;
ELSE 'No Documents required';
END CASE ;
FROM dms_report WHERE pilot = 1
End;
Here I formatted your procedure Use delimiter
DELIMITER //
CREATE PROCEDURE temp ( empId INT)
BEGIN
DECLARE var_etype VARCHAR(36);
SELECT
emptype = QOUTE(emptype)
FROM
dms_document
WHERE
id = empid;
SELECT
emptype,
CASE
WHEN emptype = 'P' THEN doctype
ELSE 'No Documents required'
END
FROM
dms_report
WHERE
pilot = 1;
End//
DELIMITER ;