I get a error on line 12 (at the endif statement).
I belief I´m doing something wrong within the IF or ELSE, can anyone help me?
DELIMITER $$
CREATE FUNCTION TEST (`param` INT)
RETURNS INT
DETERMINISTIC
BEGIN
DECLARE `var` INT;
SET `var` = 1;
IF `param` > 0 THEN
SET `var` = `var` + `param`;
END IF;
RETURN `var`;
END$$
EDIT: (same function with case instead of if, same problem)
DELIMITER $$
CREATE FUNCTION TEST (`param` INT)
RETURNS INT
DETERMINISTIC
BEGIN
DECLARE `var` INT;
SET `var` = 1;
SET `var` =
CASE
WHEN `param` > 0 THEN `var` + `param` ELSE `var`
END ;
RETURN `var`;
END$$
Try this instead:
BEGIN
DECLARE `var` INT;
SET var =
CASE
WHEN param > 0 THEN var + 1 ELSE var
END ;
RETURN var;
END$$
Found out it was a bug in PHPMyAdmin, if I added the function with 'add routine' it worked!
Related
I'm on developing web project and i get some problem with migration from oracle database to mysql database. I want to create function with this code :
DROP FUNCTION IF EXISTS F_MANIFEST_GABUNG_SMR;
DELIMITER //
CREATE FUNCTION F_MANIFEST_GABUNG_SMR (input_val varchar(4000))
RETURNS VARCHAR(4000)
BEGIN
DECLARE return_text VARCHAR(10000) DEFAULT NULL;
DECLARE not_found INT DEFAULT 0;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET not_found = 1;
DECLARE x CURSOR FOR SELECT DISTINCT IFNULL(SMR,'-') SMR FROM MANIFEST_EDI_SMR WHERE BL_NBR = input_val; OPEN x;
FETCH x INTO;
WHILE NOT_FOUND=0
DO
SET return_text = concat(ifnull(return_text, '') , ' ' , IFNULL(x.SMR, '')) ;
FETCH INTO;
END WHILE;
CLOSE ;
IF char_length(return_text) > 85 THEN
SET return_text = concat(ifnull(substr(return_text,1,85), '') , ' detail asp BL');
END IF;
RETURN return_text;
END;
//
DELIMITER ;
I am using phpmyadmin to store function with routine. Thanks for your help :)
The error message is self explanatory:you have declare the cursor after handler,need to change the order:
DROP FUNCTION IF EXISTS F_MANIFEST_GABUNG_SMR;
DELIMITER //
CREATE FUNCTION F_MANIFEST_GABUNG_SMR (input_val varchar(4000))
RETURNS VARCHAR(4000)
BEGIN
DECLARE return_text VARCHAR(10000) DEFAULT NULL;
DECLARE not_found INT DEFAULT 0;
-- Declare cursor before handler
DECLARE x CURSOR FOR SELECT DISTINCT IFNULL(SMR,'-') SMR
FROM MANIFEST_EDI_SMR WHERE BL_NBR = input_val; OPEN x;
-- handler need to be after cursor
DECLARE CONTINUE HANDLER FOR NOT FOUND SET not_found = 1;
FETCH x INTO;
WHILE NOT_FOUND=0
DO
SET return_text = concat(ifnull(return_text, '') , ' ' , IFNULL(x.SMR, '')) ;
FETCH INTO;
END WHILE;
CLOSE ;
IF char_length(return_text) > 85 THEN
SET return_text = concat(ifnull(substr(return_text,1,85), '') , ' detail asp BL');
END IF;
RETURN return_text;
END;
//
DELIMITER ;
More details can be found at:https://dev.mysql.com/doc/refman/8.0/en/cursors.html
I am trying to create the below function in MySQL but getting syntax error.
I am not able to find the solution, would be grateful for some help
CREATE FUNCTION `test`.`pro`(depart_id int) RETURNS varchar
BEGIN
DECLARE title varchar;
if depart_id = 1 then
set title='IT Department';
else if depart_id = 2 then
set title='HR Department';
else
set title='Admin';
end if;
return title;
END$$
DELIMITER ;
You have several syntax errors in your script:
varchar must have a length
You should define DELIMITER $$ first
It's not else if, but elseif
Try this;)
DELIMITER $$
CREATE FUNCTION `test`.`pro`(depart_id int) RETURNS varchar(10)
BEGIN
DECLARE title varchar(10);
if depart_id = 1 then
set title='IT Department';
elseif depart_id = 2 then
set title='HR Department';
else
set title='Admin';
end if;
return title;
END $$
DELIMITER ;
I'm using phpmyadmin and MySQL to run a simple query, that creates a function checking the existence of a certain record. It keeps throwing a syntax error at line 7 with Declare. I have no idea why. I did try to use the built-in function creator, but it's messed up and I don't like it. Any help appreciated!
CREATE FUNCTION check_if_card_exists (_name TEXT)
RETURNS INT
DETERMINISTIC
READS SQL DATA
BEGIN
DECLARE res INT; --line 7
IF EXISTS (SELECT `name` FROM `cards` WHERE `name` = _name)
THEN SET res = 1;
ELSE SET res = 0;
END IF;
RETURN res;
END
Try the following:
DELIMITER $$
DROP FUNCTION IF EXISTS `check_if_card_exists`$$
CREATE FUNCTION check_if_card_exists (_name TEXT)
RETURNS INT
DETERMINISTIC
READS SQL DATA
BEGIN
DECLARE res INT; --line 7
IF EXISTS (SELECT `name` FROM `cards` WHERE `name` = _name)
THEN SET res = 1;
ELSE SET res = 0;
END IF;
RETURN res;
END$$
DELIMITER ;
I have browsed around and other solutions don't seem to be working for me here. I keep getting a 'No return found for functon' error when trying to create this function in MySQL. Any idea why?
CREATE FUNCTION `mydb`.`myfunction`(Name varchar(20))
RETURNS int
LANGUAGE SQL
NOT DETERMINISTIC
SELECT SUM(Transaction.Bought) INTO #Qty FROM Transaction WHERE Transaction.Name = Name;
RETURN #Qty
Try this
DELIMITER $$
CREATE FUNCTION `myfunction`(`Name` VARCHAR(20) CHARSET utf8) RETURNS INT NOT DETERMINISTIC
READS SQL DATA
MAIN: BEGIN
DECLARE returnVal int;
SELECT SUM(`Transaction`.Bought) INTO returnVal FROM `Transaction` WHERE `Transaction`.Name = Name;
RETURN returnVal;
END MAIN;$$
DELIMITER ;
Alternatively, try this,
DELIMITER $$
CREATE FUNCTION `myfunction`(Name varchar(20))
RETURNS int
LANGUAGE SQL
NOT DETERMINISTIC
BEGIN
DECLARE returnVal int;
SELECT SUM(`Transaction`.Bought) INTO returnVal
FROM `Transaction`
WHERE `Transaction`.Name = Name;
RETURN returnVal;
END$$
DELIMITER ;
delimiter //
create function myfun1234 (i int,j int)
returns int
begin
declare x int ;
declare y int ;
declare z int ;
set x=10;
set y=20;
if (1<=x && j>=y )then
set z=i+j;
end if;
return z;
end; //
-- error1 FUNCTION myfun12 ended without RETURN
I have been sitting with a stored procedure for MySQL for days now, it just won't work, so I thought I'd go back to basic and do a very simple function that checks if an item exists or not.
The problem I had on the first one was that it said END IF is invalid syntax on one of my IF clauses, but not the other two. The second one won't even recognize BEGIN as valid syntax...
Is it I that got everything wrong, or have I stumbled upon a MYSQL Workbench bug? I have Workbench 5.2 (latest version when I'm writing this) and this is the code:
DELIMITER $$
CREATE FUNCTION `filmsidan`.`f_lateornot` (movie_id INT)
BEGIN
DECLARE check_val INT;
DECLARE return_val INT;
SELECT stockId
FROM orders
WHERE stockId = movie_id
INTO check_val;
IF check_val <= 0
THEN
SET return_val = 1;
ELSE
SET return_val = 0;
END IF;
RETURN return_val;
END
to fix the "begin" syntax error, you have to declare a return value, like this:
CREATE FUNCTION `filmsidan`.`f_lateornot` (movie_id INT) RETURNS INT(11)
after doing that, Workbench won't return an error anymore ;o)
You have to specify the return value in signature as well delimiter at the end is missing. So, your function should look like
DELIMITER $$
CREATE FUNCTION `filmsidan`.`f_lateornot` (movie_id INT) RETURNS INT
BEGIN
DECLARE check_val INT;
DECLARE return_val INT;
SELECT stockId
FROM orders
WHERE stockId = movie_id
INTO check_val;
IF check_val <= 0
THEN
SET return_val = 1;
ELSE
SET return_val = 0;
END IF;
RETURN return_val;
END
$$
DELIMITER $$
CREATE FUNCTION `filmsidan`.`f_lateornot` (movie_id INT)
BEGIN
DECLARE check_val INT;
DECLARE return_val INT;
SELECT stockId
FROM orders
WHERE stockId = movie_id
INTO check_val;
IF check_val <= 0
THEN
SET return_val = 1;
ELSE
SET return_val = 0;
END IF;
RETURN return_val;
END
$$
DELIMITER ;
Add this last thing it works :
$$
DELIMITER ;
it means you are using ( ; ) this in function so for that reason we use it..see
and see also
MySQL - Trouble with creating user defined function (UDF)