Can't Create MySQL TRIGGER when i use IS NOT NULL - mysql

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;//

Related

#1064 - You have an error in your SQL syntax at line 7

enter code here
DELIMITER $$
DROP TRIGGER IF EXISTS `Update_Status`$$
CREATE TRIGGER `Update_Status` AFTER INSERT ON `occurance_time`
FOR EACH ROW BEGIN
IF NOT EXISTS (SELECT `F_Seen` FROM `Total_Hours` WHERE (`SSN`=new.`SSN` and `Day_Date`=new.`Day_Date`))
THEN
INSERT INTO `total_time` (`SSN`,`Name`,`Day_Date`,`F_Seen`) VALUES(new.`SSN`,new.`Name`,new.`Day_Date`,new.`Cap_time`);
ELSE
UPDATE `total_time` SET(`L_Seen`=new.`Cap_time`) WHERE (`SSN`=new.`SSN` and `Day_Date`=new.`Day_Date`);
END$$
I have Created this After insert trigger On Occurrence _time I want to store first time of occurrence of a day and last time of occurrence of Day in Total_time table but getting this Error
"1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '(L_Seen=new.Cap_time) WHERE (SSN=new.SSN and Day_Date=new.`Day_Date...' at line 7" ***
Instead of SET(L_Seen=new.Cap_time) Please use SET L_Seen=new.Cap_time. You missed end if also. Below code is working.
CREATE TRIGGER `Update_Status` AFTER INSERT ON `occurance_time`
FOR EACH ROW
BEGIN
IF NOT EXISTS (SELECT `F_Seen` FROM `Total_Hours` WHERE `SSN`=new.`SSN` and `Day_Date`=new.`Day_Date`)
THEN
INSERT INTO `total_time` (`SSN`,`Name`,`Day_Date`,`F_Seen`) VALUES(new.`SSN`,new.`Name`,new.`Day_Date`,new.`Cap_time`);
ELSE
UPDATE `total_time` SET `L_Seen`=new.`Cap_time` WHERE `SSN`=new.`SSN` and `Day_Date`=new.`Day_Date`;
end if;
end
DB-Fiddle:
create table occurance_time(SSN varchar(50),Name varchar(50),Day_Date date,Cap_time time);
CREATE TRIGGER Update_Status AFTER INSERT ON occurance_time
FOR EACH ROW BEGIN
IF NOT EXISTS (SELECT F_Seen FROM total_time WHERE SSN=new.SSN and Day_Date=new.Day_Date)
THEN
INSERT INTO total_time (SSN,Name,Day_Date,F_Seen) VALUES(new.SSN,new.Name,new.Day_Date,new.Cap_time);
ELSE
UPDATE total_time SET L_Seen=new.Cap_time WHERE SSN=new.SSN and Day_Date=new.Day_Date;
end if;
end
db<>fiddle here

Trigger (AFTER/BEFORE) In Mysql

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

MYSQL Error when creating a trigger

I have the following MySQL trigger query :
CREATE
TRIGGER `after_insert_stock` AFTER INSERT
ON `stock`
FOR EACH ROW BEGIN
IF NEW.deleted THEN
SET #changetype = 'DELETE';
ELSE
SET #changetype = 'NEW';
END IF;
INSERT INTO stock_audit (stock_id, commodity_name,commodity_id,delivery_no,supplier_name,batch_no,expiry_date,units_per_pack,no_of_packs,total_quantity,buying_price,selling_price,remarks,available_quantity,user_id,changetype) VALUES (NEW.stock_id,NEW.commodity_name,NEW.commodity_id,NEW.delivery_no,NEW.supplier_name,NEW.batch_no,NEW.expiry_date,NEW.units_per_pack,NEW.total_quantity,NEW.buying_price,NEW.selling_price,NEW.remarks,NEW.available_quantity,NEW.user_id, #changetype);
END$$
When I run the Query I get the following MySQL error : Error
SQL query:
CREATE TRIGGER `after_insert_stock` AFTER INSERT ON `stock_audit`
FOR EACH
ROW BEGIN
IF NEW.deleted
THEN
SET #changetype = 'DELETE';
MySQL said: Documentation
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 7
Please can some one help solving the problem?
You've missed DELIMITER declaration:
DELIMITER $$
CREATE
TRIGGER `after_insert_stock` AFTER INSERT
ON `stock`
FOR EACH ROW BEGIN
IF NEW.deleted THEN
SET #changetype = 'DELETE';
ELSE
SET #changetype = 'NEW';
END IF;
INSERT INTO stock_audit (stock_id, commodity_name,commodity_id,delivery_no,supplier_name,batch_no,expiry_date,units_per_pack,no_of_packs,total_quantity,buying_price,selling_price,remarks,available_quantity,user_id,changetype) VALUES (NEW.stock_id,NEW.commodity_name,NEW.commodity_id,NEW.delivery_no,NEW.supplier_name,NEW.batch_no,NEW.expiry_date,NEW.units_per_pack,NEW.total_quantity,NEW.buying_price,NEW.selling_price,NEW.remarks,NEW.available_quantity,NEW.user_id, #changetype);
END$$
Try this:
DELIMITER $$
CREATE TRIGGER after_insert_stock AFTER INSERT ON stock FOR EACH ROW BEGIN
DECLARE changetype varchar;
IF NEW.deleted THEN
SET changetype := 'DELETE';
ELSE
SET changetype := 'NEW';
END IF;
INSERT INTO stock_audit (stock_id, commodity_name,commodity_id,delivery_no,supplier_name,batch_no,expiry_date,units_per_pack,no_of_packs,total_quantity,buying_price,selling_price,remarks,available_quantity,user_id,changetype) VALUES (NEW.stock_id,NEW.commodity_name,NEW.commodity_id,NEW.delivery_no,NEW.supplier_name,NEW.batch_no,NEW.expiry_date,NEW.units_per_pack,NEW.total_quantity,NEW.buying_price,NEW.selling_price,NEW.remarks,NEW.available_quantity,NEW.user_id, :changetype);
END$$
Try this simplified query -
CREATE TRIGGER `after_insert_stock`
AFTER INSERT
ON `stock`
FOR EACH ROW
INSERT INTO
stock_audit (stock_id, commodity_name,commodity_id,delivery_no,supplier_name,batch_no,expiry_date,units_per_pack,no_of_packs,total_quantity,buying_price,selling_price,remarks,available_quantity,user_id,changetype)
VALUES
(NEW.stock_id,NEW.commodity_name,NEW.commodity_id,NEW.delivery_no,NEW.supplier_name,NEW.batch_no,NEW.expiry_date,NEW.units_per_pack,NEW.total_quantity,NEW.buying_price,NEW.selling_price,NEW.remarks,NEW.available_quantity,NEW.user_id, IF(NEW.deleted, 'DELETE', 'NEW'));

Else-If not working in trigger definition

#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 12
delimiter |
CREATE TRIGGER `pointhistorytrigger` AFTER UPDATE ON `points`
FOR EACH ROW BEGIN
IF NEW.is_open='4'
THEN
INSERT into point_history (`idpoints`,`idmembers`,`action_time`,`action_type`)
VALUES (OLD.idpoints,NEW.idmembers,NOW(),'3');
ELSE IF NEW.is_open='3'
THEN
INSERT into point_history (`idpoints`,`idmembers`,`action_time`,`action_type`)
VALUES (OLD.idpoints,NEW.idmembers,NOW(),'2');
END IF;
END;
| delimiter ;
delimiter |
CREATE TRIGGER `pointhistorytrigger`
AFTER UPDATE ON `points`
FOR EACH ROW
BEGIN
IF NEW.is_open='4' THEN
INSERT into point_history (`idpoints`,`idmembers`,`action_time`,`action_type`)
VALUES (OLD.idpoints,NEW.idmembers,NOW(),'3');
ELSEIF NEW.is_open='3' THEN
INSERT into point_history (`idpoints`,`idmembers`,`action_time`,`action_type`)
VALUES (OLD.idpoints,NEW.idmembers,NOW(),'2');
END IF;
END | -- <<== remove the semi colon here
delimiter ;
MySQL IF Syntax
If you are using an else-if in MySQL you have to use ELSEIF instead of ELSE IF. Everything else is fine in your statement.
You need 2 end if , one for if and another for else if...So your final trigger will be like this..
DELIMITER |
CREATE TRIGGER `pointhistorytrigger` AFTER UPDATE ON `points` FOR EACH ROW
BEGIN
IF NEW.is_open='4' THEN
INSERT into point_history (`idpoints`,`idmembers`,`action_time`,`action_type`) VALUES (OLD.idpoints,NEW.idmembers,NOW(),'3');
ELSE IF NEW.is_open='3' THEN
INSERT into point_history (`idpoints`,`idmembers`,`action_time`,`action_type`) VALUES (OLD.idpoints,NEW.idmembers,NOW(),'2');
END IF;
END IF;
END;

Trigger syntax error

My first time using triggers. Can anyone please explain why this trigger won't work? The error I'm getting is inconclusive (error near '' at line 5)
create trigger queue after update on downloads
for each row
begin
if NEW.completed = 1 then
insert into s_queue ( website_id ) values ( NEW.website_id );
end if;
end;
You need to change the delimiter from ; to something else, before defining any stored procedure/functions or triggers.
delimiter ||
create trigger queue after update on downloads
for each row
begin
if NEW.completed = 1 then
insert into s_queue ( website_id ) values ( NEW.website_id );
end if;
end||
delimiter;