Mysql Error when creating trigger - mysql

error note: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 '#sum int default 0;
set #sum=(select count(*) from inserted);
if #sum>1 then
' at line 5
and this is the code:
delimiter //
create trigger insert_only_one
after insert on sc
for each row
begin
declare #sum int default 0;
set #sum=(select count(*) from inserted);
if #sum>1 then
print('dont insert more than one record');
rollback transaction
end
The error note shows I have error at line 5.
I tried int(5) or just 'int', with or without default 0 still can't work.

You don't need to declare variable because you are using #sum variable that is initialized automatically..
you can't print anything inside a trigger...
Also your command rollback transaction is Invalid for MySQL.
Use only rollback with semicolon..

Related

getting an error while running a stored procedure ERROR 1064 (42000)

CREATE PRODECURE prc_inv_amounts
#inv_num DECIMAL
BEGIN
UPDATE INVOICE SET
INV_SUBTOTAL = (SELECT SUM(LINE_TOTAL) FROM LINE WHERE INV_NUMBER = #inv_num),
INV_TAX = INV_SUBTOTAL * 0.8,
INV_TOTAL = INV_SUBTOTAL + INV_TAX
WHERE INV_NUMBER = #inv_num
END
Error:
ERROR 1064 (42000) at line 1: 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 'PRODECURE prc_inv_amounts #inv_num DECIMAL BEGIN UPDATE INVOICE SET INV_SUBTOTAL' at line 1
It looks like you have taken some sqlserver code and dumped it into mysql. Unfortunately sqlserver and mysql are quite different in SP syntax and variable declaration.
The code translated to mysql is
DELIMITER $$
CREATE PROcedURE prc_inv_amounts(inv_num int)
BEGIN
declare vinv_subtotal decimal(10,2);
SELECT SUM(LINE_TOTAL) into vinv_subtotal FROM LINE WHERE INV_NUMBER = inv_num;
UPDATE INVOICE SET
INV_SUBTOTAL = vinv_subtotal,
INV_TAX = vINV_SUBTOTAL * 0.8,
INV_TOTAL = vINV_SUBTOTAL + (vINV_SUBTOTAL * 0.8)
WHERE INV_NUMBER = inv_num;
END $$
delimiter ;
Note the use of delimiters https://dev.mysql.com/doc/refman/8.0/en/stored-programs-defining.html , the input parameters must be defined in brackets as part of the create statement https://dev.mysql.com/doc/refman/8.0/en/create-procedure.html, user defined an declared variables are not the same How to declare a variable in MySQL?, every statement must be terminated with a ; (semi colon)
I have also moved the sub total calculation.

mysql stored procedure declare error

I don't really get the error ...
i put the declare statement right after begin. i usually do a lot of MS SQL so i am not that practiced in mysql syntax. please help me out
DELIMITER //
CREATE DEFINER=`root`#`localhost` PROCEDURE `AddEntry2`(IN `inDate` DATE, IN `inUser` INT, IN `inComment` TEXT, IN `inStart` INT, IN `inEnd` INT)
BEGIN
DECLARE commID UNSIGNED;
INSERT IGNORE INTO dimcomment (comment) values (inComment);
SELECT commID := MAX(id)
FROM dimcomment
WHERE comment = inComment;
INSERT INTO factentry (FKDate, FKUser, FKComment,FKStart,FKEnd,FKAbsence)
VALUES (
inDate,
inUser,
commID,
inStart,
inEnd,
1
);
END //
Error I get:
#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 'UNSIGNED;
INSERT IGNORE INTO dimcomment (comment) values (inComment);
SELECT c' at line 3
well i changed UNSIGNED to INT and Error Message changed:
#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 ':= MAX(id)
FROM dimcomment
' at line 5
Seems like i cannot set via SELECT VAR := VALUE ?

Loops in stored procedures that won't execute

I've read many threads here about mysql loops that throw errors. So do mine but neither of the solutions I've tried seem to fix my problem.
I'm working with some stored procedures and I'm trying to get the following loop to work:
DECLARE counter INT(11) DEFAULT 0;
add_rows: LOOP
INSERT INTO vote_orders (id, vote_id, vote_order, vote_candidate)
WHILE counter <= #number_of_candidates DO
SELECT NULL, vote_id, counter, vote_candidate_a
FROM votes_copy
IF counter = #number_of_candidates THEN
LEAVE add_rows;
END IF;
UNION
END WHILE;
END LOOP add_rows;
This always throws the following 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 'DECLARE counter INT(11) DEFAULT 0;
add_rows: LOOP
INSERT INTO vote_orde' at line 30
I've tried to only store the declaration of 'counter' which seems to work perfect but as soon as I try to add the loop it fails. (#number_of_candidates is a stored procedure that's called from the loop, the UNION is supposed to union each selects until counter reaches #number_of_candidates).
Do you have any suggestions on why this won't work?

Weird issue with a stored procedure in MySQL

I need to add a new stored procedure on our company's MySQL server. Since it's just slightly different, I used an already existing one, added the additional field and changed the name of the procedure. The weird thing now is that when I want to execute the statement, it returns:
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 3
reffering to the 0 in this line: SET #update_id := 0; What makes it weird is, that I queried that stored procedure by using SHOW CREATE PROCEDURE . It's saved in our database and is working fine. I just can't use it as a new stored procedure (no matter if I try to apply it to the new test database or if I use it on the existing database by giving it a new name).
I searched the internet for a solution. Unfortunately to no avail. I even set up a new database with a new table and some demo values where I tried to execute the original, unaltered stored procedure. It returns the exact same error.
Here's the currently used and working stored procedure I'm talking about:
CREATE DEFINER=`root`#`localhost` PROCEDURE `customer_getcard`(IN Iinstance INT, IN Itimebuy DOUBLE, IN Iprice DECIMAL(10,2), IN Itariff INT, IN Icomment VARCHAR(128))
BEGIN
SET #update_id := 0;
UPDATE customer_shop SET state = 1, id = (SELECT #update_id := id), instance=Iinstance, timebuy=Itimebuy, price=Iprice, comment=Icomment WHERE tariff=Itariff AND state = 0 LIMIT 1;
SELECT * FROM customer_shop WHERE id = #update_id;
END
I hope you guys can help me as I am completely out of ideas what's wrong. :/
Regards, Mark
You need to define an alternative command delimiter, as MySQL currently thinks your CREATE PROCEDURE command ends at the first ; it encounters (on line 3, after the 0), which would be a syntax error as it's after a BEGIN but before the corresponding END:
DELIMITER ;; -- or anything else you like
CREATE PROCEDURE
...
END;; -- use the new delimiter you chose above here
DELIMITER ; -- reset to normal
MySQL stored procedures do not use ":=" for value assignment, just use "=".
Also don't think "id = (SELECT #update_id := id)" is acceptable. Here's an alternative solution (untested):
CREATE DEFINER=`root`#`localhost` PROCEDURE `customer_getcard`(IN Iinstance INT, IN Itimebuy DOUBLE, IN Iprice DECIMAL(10,2), IN Itariff INT, IN Icomment VARCHAR(128))
BEGIN
select id into #update_id from customer_shop WHERE tariff=Itariff AND state = 0 LIMIT 1;
UPDATE customer_shop SET state = 1, instance=Iinstance, timebuy=Itimebuy, price=Iprice, comment=Icomment where id = #update_id;
SELECT * FROM customer_shop WHERE id = #update_id;
END
You may also want to put error handlers in case there's no matching row to be edited.

Stored procedures written in MySQL 5.5.8 don't work in 5.1

I have some stored procedures and a trigger that work great in MySQL 5.5.8 but for some reason don't work in 5.1. The error descriptions aren't enough for me to figure out the problem. Here is the code and the errors.
CREATE PROCEDURE `cg_getMatchingContent`(
MatchTerm VARCHAR(255),
MaxResults INT)
BEGIN
SELECT * FROM (
SELECT t.*, INSTR(t.`Title`,MatchTerm) as Pos
FROM cg_content t ) c
WHERE Pos>0 ORDER BY Pos LIMIT 0, MaxResults;
END
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 'MaxResults' at line 8
DELIMITER ;;
CREATE TRIGGER `cg`.`cg_content_UrlDup_ConstTrig`
BEFORE INSERT ON `cg`.`cg_content`
FOR EACH ROW
Begin
DECLARE errorString VARCHAR(500);
DECLARE insert_error CONDITION FOR SQLSTATE '99001';
IF new.Url = '' THEN
SET errorString = CONCAT('Url cannot be blank
Title: ' , new.Title);
SIGNAL insert_error
SET MESSAGE_TEXT=errorString;
END if;
IF Exists(SELECT id FROM cg.cg_content WHERE Url=new.Url) THEN
SET errorString = CONCAT('Url is not unique
Title: ' , new.Title , '
Url: ' + new.Url);
SIGNAL insert_error
SET MESSAGE_TEXT=errorString;
End if;
End ;;
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 'insert_error
SET MESSAGE_TEXT=errorString;END if;IF ' at line 10
From the docs:
Within stored programs, LIMIT parameters can be specified using integer-valued routine parameters or local variables as of MySQL 5.5.6.
5.1 does not support variables in LIMIT and OFFSET.
The second one is easy to figure, hard to fix. SIGNAL and RESIGNAL commands were introduced in MySQL 5.5. You can't convert it easily to 5.1. One way to do it, would be to run a query that errors. For example a SELECT from a non-existent table.