Error with Trigger in MySQL - mysql

I'm trying to write a trigger with mysql but it gives me several error. The last one has no description (MySQL says only Error!) and so i'm here to ask for your help
delimiter |
CREATE TRIGGER SellStockTrigger
AFTER INSERT ON UtenteHaAzione
FOR EACH ROW
BEGIN
DECLARE BUD DOUBLE;
DECLARE SOMMA DOUBLE;
SET BUD=(SELECT BUDGET
FROM UTENTE
WHERE CODUTENTE = NEW.CODUTENTE);
SET SOMMA= NEW.COSTOAZIONE*NEW.QTAAZIONE;
SET BUD=BUD-SOMMA;
IF (BUD<0)
raise_application_error (6002, 'budget exceeded');
ELSE
UPDATE UTENTE
WHERE CODUTENTE=NEW.CODUTENTE
SET BUDGET=BUD;
END IF;
END;
delimiter ;
This is my database structure.
Utente_Table
UtenteHaAzione_Table
Unfortunately i used to write trigger in Oracle Databases, so i'm sure that i'm missing something!
Thanks for your help

2 things -
First after the end you need the delim something as
END; |
delimiter ;
Next the update query is wrong and should be as
UPDATE UTENTE
SET BUDGET=BUD
WHERE CODUTENTE=NEW.CODUTENTE
;

Related

MySQL need help setting up trigger

I am trying to set up a TRIGGER to clear empty strings before INSERT. Not rocket science but I can't find the error in my syntax!
Here is the TRIGGER itself
USE `ga_abtest_logging`;
DELIMITER $$
CREATE TRIGGER avoid_empty
BEFORE INSERT ON daily_analytics
FOR EACH ROW
BEGIN
IF profileID = ''
THEN SET profileID = NULL
END IF;
END$$
Here is what workbench is showing:
On hover over the END IF it reads syntax error, unexpected END, expecting ';'
Could I have a problem with the settings on my DB? I have gone through the MySQL docs and I think the trigger looks right! Does anyone see anything obviously wrong?
You should make a few changes:
Use the NEW. prefix when referencing a column value
Add a semi-colon at the end of the line where you set the value
For example:
DELIMITER $$
CREATE TRIGGER tr_b_ins_daily_analytics BEFORE INSERT ON daily_analytics FOR EACH ROW BEGIN
IF (NEW.profileID = '')
THEN
SET NEW.profileID = NULL;
END IF;
END $$
DELIMITER ;
The code below should work.
DELIMITER $$
CREATE TRIGGER avoid_empty
BEFORE INSERT ON daily_analytics
FOR EACH ROW
BEGIN
IF NEW.profileID = ''
THEN SET NEW.profileID = NULL;
END IF;
END;$$
DELIMITER ;

Why does it say this trigger contains sql syntax errors?

i have written a trigger in mysqlworkbench that should update the product stock after a new line had been added to the order table(my code is in dutch so thats why im explaining this) But it doesn't work, when i try to apply it mysqlworkbench says its sql code contains errors.
Here's the trigger:
CREATE DEFINER = CURRENT_USER TRIGGER `winkel`.`bestelregel_AFTER_INSERT` AFTER INSERT ON `bestelregel` FOR EACH ROW
BEGIN
if Product.productnr = NEW.productnr
then
Update Product
Set Hoeveelheid = (Hoeveelheid - NEW.aantal)
Where productnr = NEW.productnr;
end if;
END
What is Product in the if statement? That is, no doubt, generating a syntax error, because it is unrecognized.
I think you intend for the body to be simply:
Update Product p.
Set Hoeveelheid = (Hoeveelheid - NEW.aantal)
Where p.productnr = NEW.productnr;
The if irrelevant.
I would expect the full version to look something like:
DELIMITER $$
CREATE DEFINER = CURRENT_USER TRIGGER `winkel`.`bestelregel_AFTER_INSERT`
AFTER INSERT ON `bestelregel` FOR EACH ROW
BEGIN
UPDATE Product p
SET Hoeveelheid = (Hoeveelheid - NEW.aantal)
WHERE p.productnr = NEW.productnr;
END $$
DELIMITER ;
You have delimiter trouble. You need
DELIMITER $$
CREATE ..... TRIGGER ...
BEGIN
END$$
DELIMITER ;

get previous field value in trigger mysql

I have a trigger like this in MySQL:
DELIMITER $$
CREATE TRIGGER service_before_update BEFORE UPDATE ON service
FOR EACH ROW
BEGIN
IF NEW.seatCount < (?) THEN
SIGNAL SQLSTATE '12345';
END IF;
END$$
DELIMITER ;
instead of (?) I want previous value of "seatCount" is there any easy way or I should use something like this(ServiceNo is a PRI key):
(SELECT seatCount FROM service WHERE serviceNo = NEW.serviceNo LIMIT 1)
Yeah, it's super easy. OLD.seatCount should do what you want. http://dev.mysql.com/doc/refman/5.0/en/trigger-syntax.html has more information.

Declaring variables in PLSQL triggers in mysql

I am writing PLSQL trigger for MySQL database. I am trying to declare variables . So i am writing a declare block in the trigger. Here is my following code
CREATE TRIGGER leave_approve_trigger
AFTER UPDATE ON leave_status
FOR EACH ROW
DECLARE //Syntax error
current_id integer;
BEGIN
if NEW.status == 'APPROVED'
THEN
select id into current_id from leave_request_table;
insert into update_table values(current_id);
ENDIF;
END;
The error I get is syntax error for mysql version 5.5.0. Can we actually declare variables. Thanks in advance
The procedure syntax in a little different in MySQL, you must change some lines:
Declare block must be after begin of code.
the == operator but chanted to =
ENDIF; must change to END IF;
All block must begin with changing the delimiter for statements, and at the end restore it.
If a field name is a reserved word it can be enclosed by ` to avoid syntax error.
Here is the code:
DELIMITER $$
CREATE TRIGGER leave_approve_trigger
AFTER UPDATE ON leave_status
FOR EACH ROW
BEGIN
DECLARE current_id int;
if NEW.`status` = 'APPROVED' THEN
select id into current_id from leave_request_table;
insert into update_table values(current_id);
END IF;
END$$
DELIMITER ;
I think the MySQL version would be more like this:
delimiter $$
CREATE TRIGGER leave_approve_trigger AFTER UPDATE ON leave_status
FOR EACH ROW
BEGIN
if NEW.status = 'APPROVED' THEN
insert into update_table
select id
from leave_request_table;
END IF;
END;
delimiter ;
By the way, you don't need the variable in either Oracle or MySQL. Just use insert . . . select.
I would think that you would also want to match to a specific row in the leave_request_table, but your trigger doesn't do that.

Triggers and stored proc in MySQL

I used MSSQL stored procedures and triggers for a while; MySQL is getting me absolutely crazy about how to write even the simpler procedure.
Why I get a syntax error in this so stuoid trigger?
CREATE TRIGGER set_prio_default BEFORE INSERT ON categories
FOR EACH ROW
BEGIN
set #my_prio := 1;
SET new.prio := #my_prio;
END
In facts this TRIGGER is an oversemplification of:
DELIMITER $$
DROP PROCEDURE IF EXISTS `slot08`.`test` $$
CREATE PROCEDURE `slot08`.`test` ()
BEGIN
select 1 + max(prio) from categories INTO #my_prio;
select #my_prio;
END $$
DELIMITER ;
Still i do not understand how to use variables in procedures. If I use a DECLARE statement and the variable name miss the # character I got an error from mysql telling me "unknown system variable" - but many examples I saw used this syntax
I mean:
this does not work
CREATE TRIGGER set_prio_default BEFORE INSERT ON categories
FOR EACH ROW
BEGIN
declare my_prio integer default 1;
set my_prio := 1;
SET new.prio := my_prio;
END
If I use # I get syntax error.
Have some hints?
Thanks!
I dont think you have to use the := operator. A simple equals will do. All variables declared inside the stored procedure must be prefixed with the #symbol.
Hey, found the answer. Hope that people with so little experience as me in MySQL procedures could avoid to spend the time I have spent on the same issue. This does work:
DELIMITER $$
CREATE TRIGGER blablabla
BEFORE INSERT ON myStupidTable
FOR EACH ROW
BEGIN
declare my_prio integer;
select 1 + max(prio) from myStupidTable INTO my_prio;
SET new.prio := my_prio;
END $$
DELIMITER ;
It seems that the MySQL syntax errors experienced so far were a delimiter issue.
Greetings
Daniel