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 ;
Related
When I am trying to create a function in mysql 5.7.20, I met error 1064:
delimiter //
create function add_favorstocks (
uid_int INT,
stockid_char CHAR(20),
added_date CHAR(20)
)
returns INT
begin
declare ret INT;
case
when exists (select 1 from user_favorstocks where uid=uid_int and stockid = stockid_char)
then begin
insert into user_favorstocks (uid, stockid, added_date) values (uid_int, stockid_char, added_date);
set ret=1;
end
else
begin
case (select is_deleted from user_favorstocks where uid=uid_int and stockid = stockid_char)
when 0 then set ret=0;
else begin
update user_favorstocks set is_deleted=0, db_update_time=now() where uid=uid_int and stockid=stockid_char;
set ret=3;
end;
end
end
end
return ret
end//
delimiter ;
The error message is
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'else
begin
case (select is_deleted from user_favorstocks whe' at line 15
Does this because that I am using begin...end in an else clause?
I found the example in MySQL's documentation:
DELIMITER |
CREATE PROCEDURE p()
BEGIN
DECLARE v INT DEFAULT 1;
CASE v
WHEN 2 THEN SELECT v;
WHEN 3 THEN SELECT 0;
ELSE
BEGIN
END;
END CASE;
END;
|
It seems valid to use a begin...end statement in an else clause.
Try:
DROP FUNCTION IF EXISTS `add_favorstocks`;
delimiter //
create function add_favorstocks (
uid_int INT,
stockid_char CHAR(20),
added_date CHAR(20)
)
returns INT
begin
declare ret INT;
case when exists (select 1
from user_favorstocks
where uid=uid_int and
stockid = stockid_char)
then begin
insert into user_favorstocks
(uid, stockid, added_date)
values
(uid_int, stockid_char, added_date);
set ret=1;
-- end <-- missing a semicolon
end;
else
begin
case (select is_deleted
from user_favorstocks
where uid=uid_int and
stockid = stockid_char)
when 0 then set ret=0;
else begin
update user_favorstocks
set is_deleted=0,
db_update_time=now()
where uid=uid_int and
stockid=stockid_char;
set ret=3;
end;
-- end <-- missing CASE and a semicolon
end case;
-- end <-- missing a semicolon
end;
-- end <-- missing CASE and a semicolon
end case;
-- return ret <-- missing a semicolon
return ret;
end//
delimiter ;
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
I have created a stored procedure in which I want to list employees according to the parameter.
The procedure has some parameters, one of them I want to use for the type(eg:branch, department etc) and the other is the id corresponding to that.
I want to create where conditions to variable.
How can I do that?
DELIMITER $$
CREATE PROCEDURE Emp_details(IN div_type VARCHAR(100), IN id INT, OUT emp_name VARCHAR(100))
BEGIN
DECLARE cond VARCHAR(100);
IF div_type = 'branch' THEN
SET cond = 'branch_id='+id;
ELSEIF div_type = 'department' THEN
SET cond = 'department_id='+id;
ELSEIF div_type = 'emp' THEN
SET cond = 'employee_master_id='+id;
ELSE
SET cond = '';
END IF;
SELECT
emp_fullname INTO emp_name
FROM armr_employee_master
WHERE +cond ;
END$$
Just turn the IF into a WHERE:
SELECT emp_fullname
INTO emp_name
FROM armr_employee_master
WHERE id = CASE div_type
WHEN 'branch' THEN branch_id
WHEN 'department' THEN department_id
WHEN 'emp' THEN employee_master_id
END
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!
I have the following MySQL query:
DELIMITER //
CREATE PROCEDURE InsertResult (IN winnerID INT, IN loserID INT)
BEGIN
INSERT INTO KomperResult (WinnerID, LoserID) VALUES (#winnerID, #loserID);
DECLARE winnerScore, loserScore INT;
SELECT Score INTO #winnerScore FROM KomperPerson WHERE ID = #winnerID;
SELECT Score INTO #loserScore FROM KomperPerson WHERE ID = #loserID;
IF (#loserScore >= #winnerScore) THEN UPDATE KomperPerson SET Score = #loserScore + 1 WHERE ID = #winnerID; END IF;
END//
I get an error on:
DECLARE winnerScore, loserScore INT;
What am I doing wrong?
DECLAREs need to go on the first line of your procedure.
From the docs:
DECLARE is permitted only inside a
BEGIN ... END compound statement and
must be at its start, before any other
statements.