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 ;
Related
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 a mysql function that returns a varchar value. Inside this function I have define a cursor which only gives a single value. This means in my select statement, I have taken a specific value using table primary key combination. Since I know that this cursor only return one value I don't want to add a loop to check whether the cursor return a value or not.
DELIMITER //
CREATE FUNCTION PROGRAM_API_Get_Name(
program_id_ VARCHAR(15),
uni_id_ VARCHAR(15),
fac_id_ VARCHAR(15)) RETURNS VARCHAR(100)
BEGIN
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
DECLARE degree_name_ VARCHAR(100);
DECLARE get_name_ CURSOR FOR
SELECT program_name
FROM degree_program_tab
WHERE program_id = program_id_
AND uni_id = uni_id_
AND fac_id = fac_id_;
OPEN get_name_;
IF(!done) THEN
FETCH get_name_ INTO degree_name_;
CLOSE get_name_;
RETURN degree_name_;
END IF;
RETURN NULL;
END//
This function gives me the following error
Error Code: 1064. 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 'BEGIN DECLARE CONTINUE HANDLER FOR
NOT FOUND SET done = TRUE; ' at line 5 0.
If you know how to overcome this, Please help me
I found an answer for this,
cursor.rowcount is the solution
DELIMITER //
CREATE FUNCTION PROGRAM_API_Get_Name(
program_id_ VARCHAR(15),
uni_id_ VARCHAR(15),
fac_id_ VARCHAR(15)) RETURNS VARCHAR(100)
BEGIN
DECLARE degree_name_ VARCHAR(100);
DECLARE get_name_ CURSOR FOR
SELECT program_name
FROM degree_program_tab
WHERE program_id = program_id_
AND uni_id = uni_id_
AND fac_id = fac_id_;
OPEN get_name_;
IF(get_name_.rowcount>0) THEN
FETCH get_name_ INTO degree_name_;
CLOSE get_name_;
RETURN degree_name_;
END IF;
RETURN NULL;
END//
Maybe you can avoid the cursor with a function as:
DELIMITER //
DROP FUNCTION IF EXISTS `PROGRAM_API_Get_Name`//
CREATE FUNCTION `PROGRAM_API_Get_Name` (
`program_id_` VARCHAR(15),
`uni_id_` VARCHAR(15),
`fac_id_` VARCHAR(15)
)
RETURNS VARCHAR(100)
BEGIN
RETURN (SELECT `program_name`
FROM `degree_program_tab`
WHERE `program_id` = `program_id_`
AND `uni_id` = `uni_id_`
AND `fac_id` = `fac_id_`);
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)
I have a stored function in MySQL and it works partially.
DELIMITER $$
DROP FUNCTION IF EXISTS `getsubdomain`$$
CREATE FUNCTION getsubdomain(page_id int(11))
RETURNS CHAR(255)
DETERMINISTIC
READS SQL DATA
SQL SECURITY INVOKER
BEGIN
declare current_p_id int(11);
declare current_p_parent_id int(11);
declare current_p_address_type char(255);
declare current_p_adress char(255);
SET current_p_id = page_id;
WHILE (current_p_id) <> 0
DO
select p_parent_id, p_address_type, p_adress from opu_pages where p_id = current_p_id into current_p_parent_id, current_p_address_type, current_p_adress;
IF current_p_address_type <> ''
THEN
IF current_p_address_type = 'subdomain'
THEN
RETURN current_p_adress;
ELSE
SET current_p_id = current_p_parent_id;
END IF;
ELSE
RETURN NULL;
END IF;
END WHILE;
RETURN NULL;
END$$
DELIMITER ;
If I call in query SELECT getsubdomain(p_id) FROM opu_pages; it works Ok. But if I call it in SELECT * FROM opu_pages WHERE getsubdomain(p_id)='library'; the database is collapsed and freezing.
Query and function work with one table.
What did I do wrong?
I thought that it can be caused by the table format MyISAM. But I can't change it to InnoDB because I use FULLTEXTFORMAT fields in this table.
Table opu_pages (MyISAM) scheme
p_id INT
p_parent_id INT
p_address_type ENUM (path, subdomain)
p_adress VARCHAR
Based on your post I would say that your code is entering an infinite loop for some of your input parameters.
In particular the case where p_id = p_parent_id in the opu_pages table and the current_p_address_type = 'subdomain'