Something wrong in creating mysql trigger - mysql

Hi i'm trying to create below trigger
CREATE TRIGGER TRIGBEFORE INSERT ON employee
FOR EACH
ROW
BEGIN
UPDATE employee SET userId = userId +1 WHERE userId >1;
END
it is giving me below mysql error, please suggest what is wrong in it.
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 5

you forgot to set the delimiter and misspelled a word:
delimiter |
CREATE TRIGGER TRIG BEFORE INSERT ON employee
FOR EACH ROW BEGIN
UPDATE employee SET userId = userId +1 WHERE userId >1;
END;
|
delimiter ;
If you don't set another delimiter than ; the statement will end at the first ; and your trigger definition will be incomplete. You need to tell MySQL that the stamentment should end at the delimiter you defined. After that you can set the delimiter back with delimiter ;

I think you have a typo mistake TRIGBEFORE: manual here
CREATE TRIGGER trigger_name BEFORE INSERT ON ...

Related

MySQL AFTER UPDATE TRIGGER to increase count

Any ideas what's wrong with this query? I searched around stackoverflow and it seems everyone is using this and it works.
mysql> CREATE TRIGGER upd_entry
-> AFTER UPDATE ON entry FOR EACH ROW
-> BEGIN
-> UPDATE entry
-> SET count = count +1
-> WHERE id = NEW.id
-> END;
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 'END' at line 7
Run this first to change the delimiter
DELIMITER //
create the trigger
CREATE TRIGGER upd_entry
AFTER UPDATE ON entry FOR EACH ROW
BEGIN
UPDATE entry
SET count = count +1
WHERE id = NEW.id;
END//
After change it back
DELIMITER ;
forget ; at end of line
CREATE TRIGGER upd_entry
AFTER UPDATE ON entry FOR EACH ROW
BEGIN
UPDATE entry SET count = count +1 WHERE id = NEW.id;//forget to close line ;
END;//
delimiter;
Mysql Trigger

MySQL before insert trigger filling omitted column

I am trying to make make a trigger, that will fill column B with value from column A if column B was not explicitly set in insert query. (column B is set to allow NULL and to default to NULL value)
DELIMITER $$
CREATE TRIGGER trigger_name BEFORE INSERT ON my_table FOR EACH ROW
BEGIN
IF (NEW.valueB IS NULL) THEN SET NEW.valueB = NEW.valueA ;
END
$$
But I am getting this error (not very helpful).
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 4
When I tried to locate this problematic empty string '' making the query like one word per line, mysql marked the line with '=' character as problematic.
I double checked the query for any non-ascii characters.
I am using mysql version 5.5.37-0ubuntu0.12.10.1 through commandline (eg not phpmyadmin).
You need to close the IF with END IF
DELIMITER $$
CREATE TRIGGER trigger_name BEFORE INSERT ON my_table
FOR EACH ROW
BEGIN
IF NEW.valueB IS NULL THEN
SET NEW.valueB = NEW.valueA ;
END IF ;
END;$$
delimiter ;
Check the example here http://dev.mysql.com/doc/refman/5.5/en/trigger-syntax.html
You forgot to close the IF statement. Read the syntax here. I think the code below will work for you.
DELIMITER $$
CREATE TRIGGER trigger_name BEFORE INSERT ON my_table
FOR EACH ROW
BEGIN
IF (NEW.valueB IS NULL) THEN
SET NEW.valueB = NEW.valueA ;
END IF
END $$
DELIMITER

SQL trigger giving error

I'm trying to call a stored procedure to create multiple rows using some of the cell values of the new row inserted in the table resource but I'm getting an error. Here is my query:
CREATE TRIGGER `details` AFTER INSERT ON `resource`
FOR EACH ROW BEGIN
CALL addcopies(NEW.copies, NEW.id, NEW.location);
END;
#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
What am I doing wrong?
delimiter |
CREATE TRIGGER `details` AFTER INSERT ON `resource`
FOR EACH ROW BEGIN
CALL addcopies(NEW.copies, NEW.id, NEW.location);
END
|
delimiter ;
If you don't set another delimiter than ;, then the statement will end at the first ; and your trigger definition will be incomplete. You need to tell MySQL that the statement should end at the delimiter you defined. After that you can set the delimiter back with delimiter ;

MySQL & trigger before insert

I need to create a trigger in my database.
The trigger must be able to analyse event before insert. In case of the last event already existing in the database has the same status, the trigger has to cancel the insert and update the last event.
I tried to create one but I can't add it in my database because of mysql errors:
#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 5
Here the trigger:
CREATE TRIGGER events_before_insert
BEFORE INSERT ON monitoringV2.events
FOR EACH ROW
BEGIN
DECLARE id_last_event;
DECLARE status_last_event;
SELECT id INTO id_last_event, status INTO status_last_event FROM events WHERE module_id=NEW.module_id ORDER BY serverDate DESC LIMIT 1;
IF NEW.status = status_last_event THEN
UPDATE events SET serverDate=NOW() WHERE id=id_last_event;
signal sqlstate '00000' set message_text = "Update instead of insert";
END IF;
END;
Here are information server:
PHP 5.4.6 VC9
Apache 2.4.2 VC9
MySQL 5.5.27
PhpMyAdmin 3.5.2.2
Thanks for your help
Regards,
Dorine M.
You will need to delimit your create statement:
delimiter //
CREATE...
...
END;
//
DELIMITER ;
and you will probably need to define a type for each variable declared, e.g.
DECLARE x INT;
You have to change DELIMITER before creating a trigger like this
DELIMITER $$
CREATE TRIGGER...
...
END;
$$
DELIMITER ;
You might not need to implement a trigger for this. The operation you are describing is formally known as UPSERT, i.e. update or insert.
This is how it's done in MySQL and you could also look at REPLACE.
Here is another question covering this.

MySQL Trigger ERROR in phpAdmin

Hi
Is there any error in this TRIGGER Statement.When ever i try to run this in phpAdmin its giving error saying "#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 "SELECT Count(*) into SIM_CCode_Count".I cant get what's wrong in this..please help me
This is my trigger statement
CREATE TRIGGER Is_CountryCode_There After INSERT on mr_details FOR EACH ROW
BEGIN
DECLARE SIM_CCode_Count INTEGER;
DECLARE NET_CCode_Count INTEGER;
SELECT Count(*) into SIM_CCode_Count FROM Country_Main where CountryCode=NEW.SimCntISO;
IF SIM_CCode_Count=0 THEN
INSERT INTO Country_Main(CountryCode,CountryName) Values(NEW.SIMCntISO,"Unknown");
END IF
If NEW.SimCntISO<>NEW.NetCntISO then
SELECT Count(*) into NET_CCode_Count FROM Country_Main
where CountryCode=NEW.NetCntISO
IF NET_CCode_Count=0 THEN
INSERT INTO Country_Main(CountryCode,CountryName) Values(NEW.NETCntISO,"Unknown");
END IF
END IF
END
Without proper explanation about your requirement and about tables and what you are expecting this trigger to do,its very difficult to say if any issues there in your trigger..
But as far as i can see there is some minor correction need to be done..
Try this Code and let know in detail your requirements..
CREATE TRIGGER Is_CountryCode_There After INSERT on mr_details FOR EACH ROW
BEGIN
DECLARE SIM_CCode_Count INTEGER;
DECLARE NET_CCode_Count INTEGER;
SELECT Count(*) into SIM_CCode_Count FROM Country_Main where CountryCode=NEW.SimCntISO;
IF SIM_CCode_Count=0 THEN
INSERT INTO Country_Main(CountryCode,CountryName) Values(NEW.SIMCntISO,"Unknown");
END IF;
If (NEW.SimCntISO<>NEW.NetCntISO) then
SELECT Count(*) into NET_CCode_Count FROM Country_Main
where CountryCode=NEW.NetCntISO;
IF NET_CCode_Count=0 THEN
INSERT INTO Country_Main(CountryCode,CountryName) Values(NEW.NETCntISO,"Unknown");
END IF;
End IF;
END;
You have to declare a mysql-statement delimiter before the trigger statement:
DELIMITER |
CREATE TRIGGER ...
(your code)
END|
DELIMITER ;
Otherwise MySQL interprets your ; in this statement as statement commit and executes the code immidiately. With the delimiter changed to a different character you can use the semicolon inside the trigger declaration safely.
See here: http://dev.mysql.com/doc/refman/5.0/en/create-procedure.html