how to use if condition in mysql with status = 2 - mysql

create trigger with status with passed with status = 2 and I need to create a tigger after update row second time when del_status = 2
CREATE TRIGGER `exchange_log_update` AFTER UPDATE ON `exchange`
FOR EACH ROW INSERT
IF NEW.del_status = 2
INTO
exchange_log
SET
client_id = NEW.client_id,
ex_type = NEW.ex_type,
segment_type = NEW.segment_type,
validity_from = NEW.validity_from,
validity_to = NEW.validity_to,
file_upload = NEW.file_upload,
log_status = 'update',
created_at = NEW.created_at
END IF
END
#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 'IF NEW.del_status
INTO
exchange_log
SET
client_id = NEW.client_id,
ex' at line 3

try this
CREATE TRIGGER `exchange_log_update_new` AFTER UPDATE ON `exchange`
FOR EACH ROW BEGIN
IF (NEW.del_status = 2)
THEN
INSERT INTO `exchange_log`
values(client_id = NEW.client_id,
ex_type = NEW.ex_type,
segment_type = NEW.segment_type,
validity_from = NEW.validity_from,
validity_to = NEW.validity_to,
file_upload = NEW.file_upload,
log_status = 'update',
created_at = NEW.created_at);
END IF;
END

Related

Mysql Trigger with IF and IF ELSE conditions

i have table 'comments' with fields 'type','receiver_id' ,'confirm' and 'id'.
there are three types of object that each have comment section.
after comment confirm the trigger increase the number of objects in their respective table.
DB::unprepared('CREATE TRIGGER comment_confirm
AFTER UPDATE ON comments FOR EACH ROW
BEGIN
IF OLD.confirm = 0 AND NEW.confirm = 1 THEN
IF OLD.type = profile THEN
UPDATE profiles SET comments = comments + 1 WHERE user_id = OLD.reciever_id;
ELSE IF OLD.type = blog THEN
UPDATE blogs SET comments = comments + 1 WHERE user_id = OLD.reciever_id;
ELSE IF OLD.type = topic THEN
UPDATE topics SET comments = comments + 1 WHERE user_id = OLD.reciever_id;
END IF;
END IF;
END
');
the error for migration is:
[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 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 13
Else if should be elseif (no space) IF OLD.type = profile doesn't look right should be in single quotes for a string comparison.

Trigger to check whether the record exists or not before insert in mysql

Am trying to write a trigger before inserting into a table and also I need to check if the record exists then the insert should not happen.
DELIMITER $$
DROP trigger IF EXISTS `Before_Insert_igm_vessel_details` $$
CREATE TRIGGER `Before_Insert_igm_vessel_details` BEFORE INSERT ON igm_vessel_details
FOR EACH ROW
BEGIN
IF (NOT EXISTS(SELECT * FROM igm_vessel_details WHERE VD_MESSAGE_TYPE = NEW.VD_MESSAGE_TYPE and
VD_CUSTOM_HOUSE_CODE = NEW.VD_CUSTOM_HOUSE_CODE and VD_IGM_NO = NEW.VD_IGM_NO and VD_IGM_DATE = NEW.VD_IGM_DATE and
VD_IMO_CODE_OF_VESSEL = NEW.VD_IMO_CODE_OF_VESSEL and VD_VESSEL_CODE = NEW.VD_VESSEL_CODE and VD_SHIPPING_LINE_CODE = VD_SHIPPING_LINE_CODE and
VD_SHIPPING_AGENT_CODE = NEW.VD_SHIPPING_AGENT_CODE and VD_MASTER_NAME = NEW.VD_MASTER_NAME and VD_PORT_OF_ARRIVAL = NEW.VD_PORT_OF_ARRIVAL and
VD_LAST_PORT_CALLED = NEW.VD_LAST_PORT_CALLED and VD_PORT_CALLED_BEFORE_PORT_OF_ARRIVAL1 = NEW.VD_PORT_CALLED_BEFORE_PORT_OF_ARRIVAL1 and
VD_PORT_CALLED_BEFORE_PORT_OF_ARRIVAL2 = NEW.VD_PORT_CALLED_BEFORE_PORT_OF_ARRIVAL2 and VD_VESSEL_TYPE = NEW.VD_VESSEL_TYPE and VD_TOTAL_NO_OF_LINES = NEW.VD_TOTAL_NO_OF_LINES
and VD_BRIEF_CARGO_DESCRIPTION = NEW.VD_BRIEF_CARGO_DESCRIPTION and VD_EXPECTED_ARRIVAL_DATETIME = NEW.VD_EXPECTED_ARRIVAL_DATETIME and VD_LIGHT_HOUSE_DUES = NEW.VD_LIGHT_HOUSE_DUES
and VD_SAME_BOTTOM_CARGO = NEW.VD_SAME_BOTTOM_CARGO and VD_SHIP_STORES_DECLARATION = NEW.VD_SHIP_STORES_DECLARATION and VD_CREW_LIST_DECLARATION = NEW.VD_CREW_LIST_DECLARATION and VD_PASSENGER_LIST = NEW.VD_PASSENGER_LIST
and VD_CREW_EFFECT_DECLARATION = NEW.VD_CREW_EFFECT_DECLARATION and VD_MARITIME_DECLARATION = NEW.VD_MARITIME_DECLARATION and VD_TERMINAL_OPERATOR_CODE = NEW.VD_TERMINAL_OPERATOR_CODE)) THEN
INSERT INTO fifo_test.igm_vessel_details (VD_MESSAGE_TYPE, VD_CUSTOM_HOUSE_CODE, VD_IGM_NO, VD_IGM_DATE, VD_IMO_CODE_OF_VESSEL,
VD_VESSEL_CODE,VD_SHIPPING_LINE_CODE,VD_SHIPPING_AGENT_CODE,VD_MASTER_NAME,VD_PORT_OF_ARRIVAL,
VD_LAST_PORT_CALLED,VD_PORT_CALLED_BEFORE_PORT_OF_ARRIVAL1,VD_PORT_CALLED_BEFORE_PORT_OF_ARRIVAL2,VD_VESSEL_TYPE,
VD_TOTAL_NO_OF_LINES,VD_BRIEF_CARGO_DESCRIPTION,VD_EXPECTED_ARRIVAL_DATETIME)
VALUES (NEW.VD_MESSAGE_TYPE, NEW.VD_CUSTOM_HOUSE_CODE, NEW.VD_IGM_NO, NEW.VD_IGM_DATE, NEW.VD_IMO_CODE_OF_VESSEL,
NEW.VD_VESSEL_CODE,NEW.VD_SHIPPING_LINE_CODE,NEW.VD_SHIPPING_AGENT_CODE,NEW.VD_MASTER_NAME,NEW.VD_PORT_OF_ARRIVAL,
NEW.VD_LAST_PORT_CALLED,NEW.VD_PORT_CALLED_BEFORE_PORT_OF_ARRIVAL1,NEW.VD_PORT_CALLED_BEFORE_PORT_OF_ARRIVAL2,NEW.VD_VESSEL_TYPE,
NEW.VD_TOTAL_NO_OF_LINES,NEW.VD_BRIEF_CARGO_DESCRIPTION,NEW.VD_EXPECTED_ARRIVAL_DATETIME);
END IF;
END$$
DELIMITER ;
the above script is not working.
regardless to the mysql documentation, the trigger must fails and then the insert operation will be canceled.1
try to make a signal sqlstate

Error in mysql trigger and update

I have the next trigger:
CREATE
TRIGGER t_call_status
AFTER INSERT
ON tblcdr FOR EACH ROW
BEGIN
DECLARE lastid integer;
DECLARE nseg integer;
SET lastid = LAST_INSERT_ID();
SET nseg = 120;
IF ((origen = 'ANSWERED' && destino = 'ANSWERED') && (billsec_origen >= nseg && disposition_destino >= nseg)) THEN
update tblcdr set call_status = 3 where id = lastid;
ELSE IF (origen = 'ANSWERED' && destino = 'ANSWERED') THEN
update tblcdr set call_status = 2 where id = lastid;
ELSE IF (origen = 'ANSWERED' && destino <> 'ANSWERED') THEN
update tblcdr set call_status = 1 where id = lastid;
ELSE IF (origen <> 'ANSWERED' && destino <> 'ANSWERED') THEN
update tblcdr set call_status = 0 where id = lastid;
ELSE
update tblcdr set call_status = 0 where id = lastid;
END IF;
END;
But, i got the next 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 '' at line 7
How can I fix it?
Greetings.

Syntax Issue With MySQL Trigger

DELIMITER $$
CREATE TRIGGER update_cnitrand_cust_code AFTER INSERT ON `cnitrand`
FOR EACH ROW
BEGIN
DECLARE bill_no INT DEFAULT 0;
IF NEW.bill_no = 0 THEN
SET bill_no = (SELECT bill_no FROM cnitrand
WHERE (tx_no = NEW.tx_no AND
input_area = NEW.input_area AND
tx_line_no = NEW.tx_line_no AND
bill_no <> "") OR
(tx_no = NEW.tx_no AND
bill_no <> "")
LIMIT 1);
ELSE
SET bill_no = NEW.bill_no;
END IF;
INSERT INTO tblrptcnitrandmap
(tx_no, input_area, tx_line_no, bill_cust_code, bill_no)
VALUES
(NEW.tx_no, NEW.input_area, NEW.tx_line_no, NEW.bill_cust_code, bill_no);
END;$$
It returns 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 '$$' at line 14"
Can you please tell me, what is wrong with code..
DELIMITER $$
CREATE TRIGGER update_cnitrand_cust_code AFTER INSERT ON `cnitrand`
FOR EACH ROW
BEGIN
DECLARE bill_no INT DEFAULT 0;
IF NEW.bill_no = 0 THEN
SET bill_no = (SELECT bill_no FROM cnitrand WHERE (tx_no = NEW.tx_no AND input_area = NEW.input_area AND tx_line_no = NEW.tx_line_no AND bill_no <> "") OR (tx_no = NEW.tx_no AND bill_no <> "") LIMIT 1);
ELSE
SET bill_no = NEW.bill_no;
END IF;
INSERT INTO tblrptcnitrandmap (tx_no, input_area, tx_line_no, bill_cust_code, bill_no) VALUES (NEW.tx_no, NEW.input_area, NEW.tx_line_no, NEW.bill_cust_code, bill_no);
END$$
DELIMITER ;

syntax error on updating two tables with one trigger

hi i'm trying to update two different tables with one trigger but i keep getting syntax error and i'm not sure what i'm doing wrong
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 specialitys s
JOIN speciality_objects o
ON s.id = o.speciality' at line 2
triggers works fine if i just update one table but i cant put them both in one trigger
here is my trigger
DELIMITER $$
CREATE TRIGGER after_user_type_or_status_change
AFTER UPDATE
ON users
FOR EACH ROW
BEGIN
UPDATE stats s
SET
ee_counter =
CASE
WHEN NEW.type= 1 THEN ee_counter + 1
WHEN NEW.type= 3 && OLD.type = 2 THEN ee_counter + 1
WHEN OLD.type= 1 && NEW.type != 3 THEN ee_counter - 1
ELSE ee_counter
END ,
er_counter =
CASE
WHEN NEW.type= 2 THEN er_counter + 1
WHEN NEW.type= 3 && OLD.type = 1 THEN er_counter + 1
WHEN OLD.type= 2 && NEW.type != 3 THEN er_counter - 1
ELSE er_counter
END
WHERE
s.id = 1;
END $$
BEGIN
UPDATE specialitys sp
JOIN speciality_objects o
ON sp.id = o.speciality_id
JOIN users u
ON o.user_id = u.id
SET
counter =
CASE
WHEN NEW.status = 1 THEN counter + 1
ELSE counter
END
WHERE
sp.id = o.speciality_id ;
END $$
DELIMITER ;
The END $$ in the middle is ending the trigger definition. Remove it and the following BEGIN.