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)
Related
I am working on a procedure which is doing the following:
With a cursor declared I want to go through all entries of a table to find entries where the given period is covering the one from the cursor.
Example: a student A went to school at grade x from date 3 to date 6. Let's find all who studied with him at least one time for the same class.
The while-loop would be applied to the same table. But I want to just find out if there is at least one entry or not. So the while loop should stop if the first entry appears.
Example: A student B could have visited the same class as A did. But for a year he has been in another class. Just for example.
And here is my problem. I have two variables to set and I get a syntax error.
DELIMITER $$
DROP PROCEDURE IF EXISTS coworkers$$
CREATE PROCEDURE coworkers(
IN gus INT,
IN rus INT,
OUT gto INT
)
BEGIN
DECLARE recCID INT;
DECLARE recSDT DATE;
DECLARE recEDT DATE;
DECLARE done INT DEFAULT FALSE;
DECLARE ctr INT;
DECLARE cwrk CURSOR FOR
SELECT comp_id, start_date, end_date FROM skill_cv_test WHERE usr_id = rus;
DECLARE CONTINUE HANDLER
FOR NOT FOUND
SET done = TRUE;
OPEN cwrk;
SET ctr = 0;
loop_cwrk: WHILE(ctr<1) DO
FETCH cwrk INTO recCID, recSDT, recEDT;
IF EXISTS
(SELECT *
FROM skill_cv_test AS m
WHERE m.usr_Id = gus AND m.usr_id != rus AND (m.start_date < recSDT OR m.end_date <= recEDT) AND m.comp_id = recCID)
THEN
SET ctr = 1,
SET gto = 1;
IF done THEN
LEAVE loop_cwrk;
END IF;
END WHILE loop_cwrk;
CLOSE cwrk;
end $$
delimiter ;
CALL coworkers(2,1,#gto);
I have tried different formats for the section THEN SET... which is the problem.
Here is the error code:
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 ''ctr' := 1,
SET 'gto' ;= 1;
Here you can see what else I have tried (among others). Probably it's a totally wrong approach.
Btw: the Select within the loop is only a placeholder to get the whole procedure work.
But now, what am I doing wrong?
Thanks in advance.
SOLUTION
DELIMITER $$
DROP PROCEDURE IF EXISTS coworkers$$
CREATE PROCEDURE coworkers(
IN gus INT,
IN rus INT,
OUT gto INT
)
BEGIN
DECLARE recCID INT;
DECLARE recSDT DATE;
DECLARE recEDT DATE;
DECLARE done INT DEFAULT FALSE;
DECLARE ctr INT;
DECLARE cwrk CURSOR FOR
SELECT comp_id, start_date, end_date FROM skill_cv_test WHERE usr_id = rus;
DECLARE CONTINUE HANDLER
FOR NOT FOUND
SET done = TRUE;
OPEN cwrk;
SET ctr = 0;
loop_cwrk: WHILE(ctr<1) DO
FETCH cwrk INTO recCID, recSDT, recEDT;
IF EXISTS
(SELECT *
FROM skill_cv_test AS m
WHERE m.usr_Id = gus AND m.usr_id != rus AND (m.start_date < recSDT
OR m.end_date <= recEDT) AND m.comp_id = recCID)
THEN
SET ctr = 1;
SET gto = 1;
END IF;
IF done THEN
LEAVE loop_cwrk;
END IF;
END WHILE loop_cwrk;
CLOSE cwrk;
end $$
delimiter ;
CALL coworkers(2,1,#gto);
You need add END IF for your first IF
IF EXISTS
(SELECT *
FROM skill_cv_test AS m
WHERE m.usr_Id = gus AND m.usr_id != rus AND (m.start_date < recSDT
OR m.end_date <= recEDT) AND m.comp_id = recCID)
THEN
SET ctr = 1;
SET gto = 1;
END IF; -- Add END IF here
IF done THEN
LEAVE loop_cwrk;
END IF;
Im writing a procedure, I want it to return table if there wasnt any rollbacks, and return empty table / nothing if there was at least 1 rollback.
DELIMITER $$
CREATE PROCEDURE payment(IN amount int, IN profession varchar(50))
BEGIN
DECLARE done BOOL DEFAULT FALSE;
DECLARE salary INT;
DECLARE pes VARCHAR(11);
DECLARE summary INT DEFAULT 0;
DECLARE employee_cursor CURSOR FOR (SELECT RIGHT(PESEL,3), pensja FROM Pracownicy WHERE zawod=profession);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
DROP TEMPORARY TABLE IF EXISTS tmp1;
CREATE TEMPORARY TABLE tmp1(pesel char(11), status varchar(20) DEFAULT 'wyplacono');
SET AUTOCOMMIT =0;
START TRANSACTION;
OPEN employee_cursor;
readLoop : LOOP
FETCH employee_cursor INTO pes,salary;
IF done THEN
LEAVE readLoop;
END IF;
SET summary = summary + salary;
IF( summary > amount ) THEN
ROLLBACK;
END IF;
INSERT INTO tmp1(pesel) VALUES(CONCAT('********',pes));
END LOOP;
CLOSE employee_cursor;
COMMIT;
SELECT * from tmp1;
END $$
DELIMITER ;
As far it works fine when it doesnt rollback, but
INSERT INTO tmp1(pesel) VALUES(CONCAT('********',pes));
seems to ignore transaction :/
move the rollback test to outside the loop.
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'
I have a little problem. Looks like the procedure does not exist. Somehow it's dropped after the creation. I get different error each time i change something. I'm not really sure what's causing the error, maybe I'm not allowed to drop procedures and creating them in the same query.
I hope you guys can help me out.
drop procedure if exists refIntChk;
DELIMITER //
CREATE PROCEDURE refIntChk(IN district INT(11), OUT b INT(1))
BEGIN
DECLARE b INT(1);
IF district IN (select dist FROM t13)
THEN
SET b = 1;
ELSE
SET b = 0;
END IF;
END; //
DELIMITER ;
drop procedure gen if exists ;
DELIMITER //
CREATE PROCEDURE gen()
BEGIN
DECLARE rows INT(11) DEFAULT (SELECT COUNT(dist) FROM t13);
DECLARE district INT(11);
DECLARE custname VARCHAR(16);
DECLARE revenue FLOAT;
DECLARE x INT DEFAULT 10000;
DECLARE outvar INT(11);
WHILE x > 0
DO
SET district = FLOOR(RAND()*rows)+1;
CALL refIntChk(district, outvar);
IF outvar = 1
THEN
SET custname = substring(MD5(RAND()), -16);
SET revenue = (RAND() * 10);
INSERT INTO t14 VALUES(NULL, custname, district, revenue);
SET x = x - 1;
END IF;
END WHILE;
END;//
DELIMITER ;
CALL gen();
When you get errors, it's usually good to run each statement, one by one, and see which one is producing the error.
The second DROP procedure statement should be:
drop procedure if exists gen;
Having trouble getting this to apply in MySQL Workbench 5.2.15
DELIMITER //
CREATE
DEFINER=`potts`#`%`
FUNCTION
`potts`.`fn_create_category_test` (test_arg VARCHAR(50))
RETURNS int
BEGIN
DECLARE new_id int;
SET new_id = 8;
RETURN new_id;
END//
The actual function will have a lot more between BEGIN and END but as it stands, even this 3 liner won't work.
Thanks!
DELIMITER $$
CREATE FUNCTION `fn_create_category_test` (test_arg varchar(50))
RETURNS INT
BEGIN
DECLARE new_id int;
set new_id=8;
return new_id;
END $$
DELIMITER ;
Works fine for me, try getting rid of DEFINER?