Syntax error near $$DELIMITER - mysql

i'm getting an error while creating a stored procedure, where it says that i have a syntax error, but i can't find where it is...
MySql Error:
"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 '$$DELIMITER
CREATE PROCEDURE 'entradas_sai'(IN ID_VEICULO VARCHAR(45), OUT' at
line 1
Here is the code related to the topic:
$$DELIMITER
CREATE PROCEDURE 'entradas_sai'(
IN ID_VEICULO VARCHAR(45), OUT retcode INT)
BEGIN
DECLARE '_rollback' BOOL DEFAULT 0;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET '_rollback' = 1;
START TRANSACTION;
INSERT INTO SAIDAS(data, hora) VALUES(CURDATE("yyyy-MM-dd"),CURTIME("hh:mm:ss))
UPDATE ENTRADAS(SAI) WITH VALUES(#SAI)
IF '_rollback' THEN
SET retcode = 0;
ROLLBACK;
ELSE
SET retcode = 1;
COMMIT;
END IF;
END$$
DELIMITER ;
Here is a screenshot of my MySqlWB:
EDIT:
Here is the log:
>Executing:
>USE `portaria`;
>DROP unknown IF EXISTS `unknown_SYNTAX_ERROR`;
>
>DELIMITER $$
>USE `portaria`$$
>DELIMITER $$
>
>CREATE PROCEDURE 'entradas_sai'(
>IN ID_VEICULO VARCHAR(45), OUT retcode INT)
>BEGIN
> DECLARE '_rollback' BOOL DEFAULT 0;
> DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET '_rollback' = 1;
> START TRANSACTION;
> INSERT INTO SAIDAS(data, hora) VALUES(date(now()) ,time(now()))
> UPDATE ENTRADAS(SAI) WITH VALUES(#SAI)
> IF '_rollback' THEN
> SET retcode = 0;
> ROLLBACK;
> ELSE
> SET retcode = 1;
> COMMIT;
> END IF;
>END$$
>
>DELIMITER ;$$
>
>DELIMITER ;
>
>ERROR 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 >'unknown IF EXISTS `unknown_SYNTAX_ERROR`' at line 1
>SQL Statement:
>DROP unknown IF EXISTS `unknown_SYNTAX_ERROR`
Here i got a scenarion of what i want my procedure to do...
I have a table called "Entradas" and one called "Saidas". Both have
the column "data" and "hora". With that in mind, since the "data" and
"hora column of "entradas" mean that a car joined at that date and
time, and the values from the columns "data" and "hora" of "saidas"
are mean to be inserted as i click a button, inserting the current
date and time. I'm requesting a sql syntax that could insert the
values "data" and "hora" into the table "saidas" and update a value of
"entradas" called "sai" which is equal to 0 and i want it to change to
1 on button press... Any sugestion?

delimiter symbol ($$) come after DELIMITER keyword. also use date(now()) instead CURDATE("yyyy-MM-dd") and time(now()) instead CURTIME("hh:mm:ss"))
Try This
DELIMITER $$
CREATE PROCEDURE `entradas_sai`(IN ID_VEICULO VARCHAR(45), OUT retcode INT)
BEGIN
DECLARE _rollback boolean DEFAULT 0;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET _rollback = 1;
START TRANSACTION;
INSERT INTO SAIDAS(data, hora) VALUES(date(now()) ,time(now()));
UPDATE ENTRADAS SET SAI = #SAI;
IF _rollback = 1 THEN
SET retcode = 0;
ROLLBACK;
ELSE
SET retcode = 1;
COMMIT;
END IF;
END$$
DELIMITER ;

If i'm right, you mistyped $$DELIMITER, it should be DELIMITER $$
EDIT: after adding the screenshot to the question, i believe you shouldn't quote the procedure name. I have update the code-block, try it again please.
DELIMITER $$
CREATE PROCEDURE entradas_sai(
IN ID_VEICULO VARCHAR(45), OUT retcode INT)
BEGIN
DECLARE '_rollback' BOOL DEFAULT 0;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET '_rollback' = 1;
START TRANSACTION;
INSERT INTO SAIDAS(data, hora) VALUES(CURDATE("yyyy-MM-dd"),CURTIME("hh:mm:ss"))
UPDATE ENTRADAS(SAI) WITH VALUES(#SAI)
IF '_rollback' THEN
SET retcode = 0;
ROLLBACK;
ELSE
SET retcode = 1;
COMMIT;
END IF;
END$$
DELIMITER ;

I figured what was wrong, and it wasn't only about "DELIMITER" but with a couple things more...
Here is the fixed code:
CREATE PROCEDURE entradas_sai (
IN ID_VEICULO VARCHAR(45), OUT retcode INT)
BEGIN
DECLARE _rollback BOOL DEFAULT 0;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET _rollback = 1;
START TRANSACTION;
INSERT INTO SAIDAS(data, hora) VALUES(date(now()) ,time(now()));
UPDATE ENTRADAS SET SAI=1;
IF '_rollback' THEN
SET retcode = 0;
ROLLBACK;
ELSE
SET retcode = 1;
COMMIT;
END IF;
END $$
Steps: Removed the "Delimiter $$" and "Delimiter ;" and then restructured the UPDATE query. That was the main problem, because the syntax wasn't on the spot... Thanks all who tryied to help.
In this case the value of "1" wasn't the value i want... So, i changed it to #sai, which means that the value is given at a button click, which is a increment of the parameter #sai, being equal to 1,2,3,4 and so on.

Related

MySQL Syntax Error - END IF line

I try to create a stored procedure with an if statement within.
I copied from: https://dev.mysql.com/doc/refman/5.7/en/local-variable-scope.html
But I get the following error exact on the END IF; near '':
DROP PROCEDURE IF EXISTS `myProc`;
CREATE DEFINER=`root`#`%` PROCEDURE `myProc`(
IN in_userId int,
IN in_projectId int
)
BEGIN
DECLARE tmp_courseId int;
DECLARE done TINYINT DEFAULT 0;
DECLARE cursorProjectCourse CURSOR FOR SELECT CourseId FROM XC_PROJECT_COURSE where projectId = in_projectId ;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cursorProjectCourse;
read_loop: LOOP
FETCH FROM cursorProjectCourse INTO tmp_courseId;
IF done = 1 THEN LEAVE read_loop;
END IF;
SELECT tmp_courseId, in_userId;
END LOOP;
CLOSE cursorProjectCourse;
END;
Has anyone an idea where I make a mistake?
Exact error message:
SQL 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 '' at line 19 SQL Error
MySQL Version:
5.5.46
Thanks for help!
I found the solution.
I have to set DELIMITER $$ at first statement and at the end DELIMITER ;
DELIMITER $$;
DROP PROCEDURE IF EXISTS `myProc`; $$
CREATE DEFINER=`root`#`%` PROCEDURE `myProc`(
IN in_userId int,
IN in_projectId int
)
BEGIN
DECLARE tmp_courseId int;
DECLARE done TINYINT DEFAULT 0;
DECLARE cursorProjectCourse CURSOR FOR SELECT CourseId FROM XC_PROJECT_COURSE where projectId = in_projectId ;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cursorProjectCourse;
read_loop: LOOP
FETCH FROM cursorProjectCourse INTO tmp_courseId;
IF done = 1 THEN LEAVE read_loop;
END IF;
SELECT tmp_courseId, in_userId;
END LOOP;
CLOSE cursorProjectCourse;
END;$$
DELIMITER ;
It is important to set the keyword on the first position in line. If there is a blank on the first position, the error above will be thrown.
Well you are missing the loop label while ending loop. Change it to below
read_loop: LOOP
FETCH FROM cursorProjectCourse INTO tmp_courseId;
IF done = 1 THEN
LEAVE read_loop;
END IF;
END LOOP read_loop;
SELECT tmp_courseId, in_userId;

MySQL stored Proc syntax confusion

Using syntax form docs, read many examples and docs, I have reduced code down to isolate the syntax that is causing the failure, I cannot see it... I'm still getting an error.
Running thru MySQL command line
PARAMS: A_score smallint, B_score smallint
delimiter $$
create procedure my_procedure(A_score smallint, B_score smallint)
begin
DECLARE winner BIGINT DEFAULT 0;
DECLARE winningScore, losingScore SMALLINT DEFAULT;
if A_score > B_score then
SET winningScore = 1;
elseif A_score < B_score then
SET winningScore = 2;
end if;
start transaction;
UPDATE
winners
SET
winner = winningScore
WHERE
id = 1
commit;
end $$
delimiter ;
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 ';
if A_score > B_score then
SET winningScore = 1;
elseif A_score' at line 4
I see you forgot to write SET (in the previous version of your question) to assign values to your variables.
(I changed the type for winningScore and losingScore to be characters, because smallints can't be strings):
-- Be sure to change the default delimiter before writing your procedure
delimiter $$
create procedure my_procedure(A_score smallint, B_score smallint)
begin
DECLARE winner BIGINT DEFAULT 0;
DECLARE winningScore, losingScore VARCHAR(2) DEFAULT;
if A_score > B_score then
SET winningScore = 'A';
-- ^^^--you forgot this
elseif A_score < B_score then
SET winningScore = 'B';
-- ^^^--and this
else
SET winningScore = 'AB';
-- ^^^--and this
end if;
start transaction;
-- Do whatever your transaction is meant to be
commit;
end $$
-- ^^--- And I think you're forgetting to put this
-- Be sure to reset the delimiter to ; after you end your procedure
delimiter ;
Quoting from the reference manual:
Variables can be set directly with the SET statement. See Section 13.7.4, “SET Syntax”.
Hope this helps

Set variable if null in trigger

I have a table for categories with "order" column for ordering the categories.
I'm trying to set order incrementally in trigger before insert:
CREATE TRIGGER `categories_set_order` BEFORE INSERT ON `categories` FOR EACH ROW BEGIN
DECLARE max_order INT DEFAULT 0;
SELECT MAX(categories.order) INTO max_order FROM categories;
SET NEW.order = max_order + 1;
END;
But if there are no records in the db, order column is being set to NULL.
I've modified the trigger code:
CREATE TRIGGER `categories_set_order` BEFORE INSERT ON `categories` FOR EACH ROW BEGIN
DECLARE max_order INT DEFAULT 0;
SELECT MAX(categories.order) INTO max_order FROM categories;
IF (ISNULL(#max_order)) THEN BEGIN
SET max_order = 0;
END;
SET NEW.order = max_order + 1;
END;
And I'm getting 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 '' at line 9
Line 9 is:
IF (ISNULL(#max_order)) THEN BEGIN
I've tried to remove the "#", but still getting the same error. How to fix it?
You need to change the delimiter. Otherwise the DB thinks your trigger definition ends at the first ; which would be incomplete.
Also remove the BEGIN from your IF statement and add use END IF to end your IF
delimiter |
CREATE TRIGGER `categories_set_order` BEFORE INSERT ON `categories`
FOR EACH ROW BEGIN
DECLARE max_order INT DEFAULT 0;
SELECT MAX(categories.order) INTO max_order FROM categories;
IF (ISNULL(#max_order)) THEN
SET max_order = 0;
END IF;
SET NEW.order = max_order + 1;
END
|
delimiter ;
Besides delimiter you need an END IF
DELIMITER //
CREATE TRIGGER `categories_set_order` BEFORE INSERT ON `categories` FOR EACH ROW BEGIN
DECLARE max_order INT DEFAULT 0;
SELECT MAX(categories.order) INTO max_order FROM categories;
IF (ISNULL(#max_order)) THEN
SET max_order = 0;
END IF;
SET NEW.order = max_order + 1;
END; //
DELIMITER ;

MySQL procedure loop error

When running my query I am getting this:
error:
#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 '' at line 3
SQL:
DROP PROCEDURE IF EXISTS insertPromos;
CREATE PROCEDURE insertPromos()
BEGIN
DECLARE int_val INT DEFAULT 0;
insertPromosLoop : LOOP
IF (int_val = 501) THEN
LEAVE insertPromosLoop;
END IF;
INSERT INTO `promo_code` (`code`, `valid_from` ,`valid_to` , `free_period`)VALUES (CONCAT('PROMO', int_val), '2013-10-10', '2013-11-10', 'P1M');
SET int_val = int_val +1;
END LOOP;
END;
CALL insertPromos();
You need to define a new delimiter
delimiter |
DROP PROCEDURE IF EXISTS insertPromos |
CREATE PROCEDURE insertPromos()
BEGIN
DECLARE int_val INT DEFAULT 0;
insertPromosLoop : LOOP
IF (int_val = 501) THEN
LEAVE insertPromosLoop;
END IF;
INSERT INTO `promo_code` (`code`, `valid_from` ,`valid_to` , `free_period`)VALUES (CONCAT('PROMO', int_val), '2013-10-10', '2013-11-10', 'P1M');
SET int_val = int_val +1;
END LOOP;
END
|
delimiter ;
CALL insertPromos();
Otherwise the procedure definition would end at the first ; which would not be correct.

mysql procedure questions?

when the table user delete fail,why the next sql will be exceute.who can give me a full example for mysql procedure.i want to delete example.please help?
use test;
delimiter //
create procedure proc_name(in paramter int)
begin
declare t_error int default 0;
declare continue handler for sqlexception set t_error=1;
set autocommit = 0;
START TRANSACTION;
delete from `user` where id = paramter;
delete from `user_info` where uid = paramter;
if t_error=1 then
rollback;
else
commit;
end if;
end;
//
delimiter ;
Dinel, I would change a little bit of your logic to achieve what you are looking for:
use test;
delimiter //
create procedure proc_name(in paramter int)
begin
declare continue handler for sqlexception set t_error=1;
SET #t_error = 0;
START TRANSACTION;
delete from `user` where id = paramter;
SELECT #t_error := COUNT(*) from `user` where id = paramter;
if #t_error != 0 then
ROLLBACK;
else
DELETE FROM `user_info` WHERE uid = paramter;
COMMIT;
end if;
end;
//
delimiter ;
I didn't try the syntax, but it should work. ;-)