I have a trigger which is working fine.
CREATE TRIGGER crm_listings__au
AFTER UPDATE
ON crm_listings FOR EACH ROW
INSERT INTO crm_listings_versions
SELECT
'update', NULL, NOW(), NULL, d.*
FROM
crm_listings AS d
WHERE
d.id = NEW.id;
Now I want to keep track of the field column name also. I am thinking I could not do in above query so I changed to below trigger
CREATE TRIGGER crm_listings__au
BEFORE UPDATE
ON crm_listings
FOR EACH ROW
BEGIN
IF OLD.type != NEW.type
THEN
INSERT INTO crm_listings_versions
SELECT
'update', NULL, NOW(), 'type', d.*
FROM
crm_listings AS d
WHERE
d.id = NEW.id;
END IF;
IF OLD.price != NEW.price
THEN
INSERT INTO crm_listings_versions
SELECT
'update', NULL, NOW(), 'price', d.*
FROM
crm_listings AS d
WHERE
d.id = NEW.id;
END IF;
END;
$$
When I run this code, I get this 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 '' at line 10
UPDATE:
I followed this post on stackoverflow
#kordirko: Can you please explain a little bit?
Please study the documentation: http://dev.mysql.com/doc/refman/5.7/en/stored-programs-defining.html
If you use the mysql client program to define a stored program
containing semicolon characters, a problem arises. By default, mysql
itself recognizes the semicolon as a statement delimiter, so you must
redefine the delimiter temporarily to cause mysql to pass the entire
stored program definition to the server.
To redefine the mysql delimiter, use the delimiter command. The
following example shows how to do this for the dorepeat() procedure
just shown. The delimiter is changed to // to enable the entire
definition to be passed to the server as a single statement, and then
restored to ; before invoking the procedure. This enables the ;
delimiter used in the procedure body to be passed through to the
server rather than being interpreted by mysql itself.
Simple example - I am using MySql Workbench and have copied and pasted your trigger. First some dummy tables:
create table crm_listings(
id int,
type int,
price int
);
create table crm_listings_versions(
ttype varchar(100),
something varchar(100),
d date,
something1 varchar(100),
id int,
type int,
price int
);
And now I run your code without DELIMITER
CREATE TRIGGER crm_listings__au BEFORE UPDATE ON crm_listings
FOR EACH ROW
BEGIN
IF OLD.type != NEW.type
THEN
INSERT INTO crm_listings_versions SELECT 'update', NULL, NOW(), 'type', d.* FROM crm_listings AS d WHERE d.id = NEW.id;
END IF;
IF OLD.price != NEW.price
THEN
INSERT INTO crm_listings_versions SELECT 'update', NULL, NOW(), 'price', d.* FROM crm_listings AS d WHERE d.id = NEW.id;
END IF;
END;
$$
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 10 0.000 sec
Outcome = Error
Then the same, but with the DELIMITER command:
DELIMITER $$
CREATE TRIGGER crm_listings__au BEFORE UPDATE ON crm_listings
FOR EACH ROW
BEGIN
IF OLD.type != NEW.type
THEN
INSERT INTO crm_listings_versions SELECT 'update', NULL, NOW(), 'type', d.* FROM crm_listings AS d WHERE d.id = NEW.id;
END IF;
IF OLD.price != NEW.price
THEN
INSERT INTO crm_listings_versions SELECT 'update', NULL, NOW(), 'price', d.* FROM crm_listings AS d WHERE d.id = NEW.id;
END IF;
END;
$$
DELIMITER ;
0 row(s) affected
Outcome = Success
Related
I have a problem creating a trigger in phpmyadmin. Here is my syntax:
DECLARE n_ubicacion VARCHAR(45) DEFAULT '';
IF (OLD.id_ubicacion <> NEW.id_ubicacion) THEN
SELECT ubicacion INTO n_ubicacion FROM ubicacions WHERE id = NEW.id_ubicacion LIMIT 1;
END IF;
INSERT INTO historico_equipos(usuario, id_equipo, estado, ubicacion, empleado, f_asignacion, created_at) VALUES (CURRENT_USER(), NEW.id, NEW.id_estado, n_ubicacion, NEW.id_empleado, NEW.f_asignacion, CURRENT_DATE())
I don''t know where is the problem. In phpmyadmin show the next error:
Something is wrong in your syntax near 'DECLARE n_ubicacion VARCHAR(45) DEFAULT ''; IF (OLD.id_ubicacion <> NEW.id_ubicacion) THEN'
Thanks.
Check if any other statements are present inside the Trigger above the DECLARE statement. If so, then it may throw the error.
Check your delimiter. By default, MySQL's delimiter is ';'. So, on seeing the first ';', your query will break. Hence use some other delimiter before creating the trigger.
Example:
DELIMITER //
CREATE TRIGGER ...
BEGIN
DECLARE n_ubicacion VARCHAR(45) DEFAULT '';
IF (OLD.id_ubicacion <> NEW.id_ubicacion) THEN
SELECT ubicacion INTO n_ubicacion FROM ubicacions WHERE id =
NEW.id_ubicacion LIMIT 1;
END IF;
INSERT INTO historico_equipos(usuario, id_equipo, estado, ubicacion, empleado, f_asignacion, created_at) VALUES (CURRENT_USER(), NEW.id, NEW.id_estado, n_ubicacion, NEW.id_empleado, NEW.f_asignacion, CURRENT_DATE())
END//
Then again change the DELIMITER to default.
DELIMITER ;
Please refer the below image
I want to create an After Insert trigger on Ongoing_Fees table where it updates the VAT table. However before it insert data to the VAT table, it should check whether the VAT field of the particular Portfolio is true. Ongoing_Fees table has the foreign key access to the Portfolio table, so it can access the related portfolio.
Below is what I have tried
DELIMITER $$
CREATE TRIGGER `Ongoing_Fees_AINS` AFTER INSERT ON `Ongoing_Fees` FOR EACH ROW
BEGIN
DECLARE `vat` Boolean;
`vat` = SELECT VAT from Portfolio WHERE Portfolio.idPortfolio = New.idPortfolio;
IF (`vat` == true) THEN
INSERT INTO VAT VALUES (100, current_Timestamp, New.idPortfolio, New.idOngoing_Fees)
END;
But of course, it has an 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 '= SELECT VAT from Portfolio WHERE Portfolio.idPortfolio = New.idPortfolio;
Try:
DELIMITER $$
CREATE TRIGGER `Ongoing_Fees_AINS` AFTER INSERT ON `Ongoing_Fees`
FOR EACH ROW
BEGIN
DECLARE `_vat` BOOL;
SET `_vat` := (SELECT `VAT` FROM `Portfolio` WHERE `Portfolio`.`idPortfolio` = NEW.`idPortfolio`);
IF (`_vat` = TRUE) THEN
INSERT INTO `VAT` (
`Amount`,
`current_timestamp`,
`Portfolio_idPortfolio`,
`Ongoing_Fees_idOngoing`
)
VALUES (
100,
CURRENT_TIMESTAMP(),
NEW.`idPortfolio`,
NEW.`idOngoing_Fees`
);
END IF;
END$$
DELIMITER ;
A shorter version:
DELIMITER $$
CREATE TRIGGER `Ongoing_Fees_AINS` AFTER INSERT ON `Ongoing_Fees`
FOR EACH ROW
BEGIN
INSERT INTO `VAT` (
`Amount`,
`current_timestamp`,
`Portfolio_idPortfolio`,
`Ongoing_Fees_idOngoing`
)
SELECT
100,
CURRENT_TIMESTAMP(),
NEW.`idPortfolio`,
NEW.`idOngoing_Fees`
FROM
DUAL
WHERE
TRUE = (SELECT
`VAT`
FROM
`Portfolio`
WHERE
`Portfolio`.`idPortfolio` = NEW.`idPortfolio`
);
END$$
DELIMITER ;
CREATE TRIGGER b_I_O AFTER UPDATE ON book
FOR EACH ROW
BEGIN
IF (OLD.status IS NOT NULL AND NEW.status IS NOT NULL AND NEW.status != OLD.status ) THEN
INSERT INTO book_in_out (astatus, bid, time) VALUES(NEW.status, OLD.id, NOW());
END IF;
END;
First post here after hours of searching.
I'm trying to insert into book_in_out using trigger after updating booking table, I was able to do this but if I update bookName the trigger will try to run.
I only want the trigger to run only when status column changes. default value for status column is NULL
Below is the error i keep getting:
#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 may try this
delimiter //
CREATE TRIGGER b_I_O AFTER UPDATE ON book
FOR EACH ROW
BEGIN
IF (OLD.status IS NOT NULL AND NEW.status IS NOT NULL AND NEW.status != OLD.status ) THEN
INSERT INTO book_in_out (`astatus`, `bid`, `time`) VALUES(NEW.status, OLD.id, NOW());
END IF;
END;//
delimiter ;
Try to manually specify delimeter for your trigger so it won't be confused with semicolons used within BEGIN/END, i.e.
DELIMITER //
CREATE TRIGGER b_I_O AFTER UPDATE ON book
FOR EACH ROW
BEGIN
IF (OLD.status IS NOT NULL AND NEW.status IS NOT NULL AND NEW.status != OLD.status ) THEN
INSERT INTO book_in_out (astatus, bid, time) VALUES(NEW.status, OLD.id, NOW());
END IF;
END;//
I want to check the existance of specific record in db table, if it's exist then update if not I want to add new record
I am using stored procedures to do so, First I make update stetement and want to check if it occurs and return 0 then there's no record affected by update statement and that means the record does not exist.
I make like this
DELIMITER //
CREATE PROCEDURE revokePrivilegeFromUsers(IN userId int(11), IN privilegeId int(11), IN deletedBy int(11))
BEGIN
DECLARE isExist int;
isExist = update `user_privileges` set `mode` ='d' ,`updated_by` = deletedBy, `date_time_assigned` = CURRENT_TIMESTAMP() where `user_id`= userId and `privilege_id`=privilegeId;
IF isExist == 0 THEN
insert into `user_privileges`(`user_id`,`privilege_id`,`mode`,`date_time_assigned`,`updated_by`)values (userId ,privilegeId ,'d',CURRENT_TIMESTAMP(),deletedBy );
END IF;
END //
DELIMITER ;
This error occur with me
#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 '= update `user_privileges` set `mode` ='d' ,`updated_by` = deletedBy, `date_time' at line 6
Is the way I am working is supported by mysql?
I solve the problem, I had 2 prblems
ROW_COUNT() is used to get the number of rows affected in insert, update or delete statements.
Equals comparison in stored procedure is = not ==
The correct stored procedure is
DELIMITER //
CREATE PROCEDURE revokePrivilegeFromUsers(IN userId int(11), IN privilegeId int(11), IN deletedBy int(11))
BEGIN
DECLARE count int default -1;
update `user_privileges` set `mode` ='d' ,`updated_by` = deletedBy, `date_time_assigned` = CURRENT_TIMESTAMP() where `user_id`= userId and `privilege_id`=privilegeId;
SELECT ROW_COUNT() into count ;
IF count = 0 THEN
insert into `user_privileges`(`user_id`,`privilege_id`,`mode`,`date_time_assigned`,`updated_by`)values (userId ,privilegeId ,'d',CURRENT_TIMESTAMP(),deletedBy );
END IF;
END //
DELIMITER ;
Use the INSERT IGNORE statement instead. I assume that your table has (user_id, privilege_id) as a unique key.
insert ignore into user_privileges (user_id,privilege_id,`mode,date_time_assigned,updated_by)
values (userId ,privilegeId ,'d',CURRENT_TIMESTAMP(),deletedBy )
on duplicate key update mode='d', date_time_assigned=now(),updated_by=deletedBy
im creating stored procedure in my sql but getting error,error shows as below,help me Error Descrption:You hve an error in your sql syntax: ckeck the manual that corresponds to your MYSQL
server version fro the right syntax to use near 'END' at line 13
DELIMITER $$
DROP PROCEDURE IF EXISTS myhealthcamp.area $$
CREATE PROCEDURE myhealthcamp.area
(
IN id INT,
IN ar VARCHAR(45)
)
BEGIN
if exists (select area_id from area where area_id = id)
Then
update area set areaname = ar where area_id=id;
else
insert into area(area_id, areaname) values(id,ar);
END IF
END $$
DELIMITER ;
You're missing a ; in this line:
END IF
It should be:
END IF;
IF is a statement and all statements must end in semi-colons. This one's no different. See the MySQL documentation for IF.
DROP PROCEDURE IF EXISTS myhealthcamp.area $$
CREATE PROCEDURE myhealthcamp.area
(
IN id INT,
IN ar VARCHAR(45)
)
BEGIN
if exists (select area_id from area where area_id = id)
Then
update area set areaname = ar where area_id=id;
else
insert into area(area_id, areaname) values(id,ar);
END IF;
semicolon missing
Thanks
Sanil