In addition to the notes table, I am applying this same trigger to around 15 other tables. Not sure, but seems like a stored procedure will eliminate the duplicated code. Is this a good application for a stored procedure? If so, how could it be implemented?
CREATE TRIGGER tg_notes_ins BEFORE INSERT ON notes FOR EACH ROW
BEGIN
IF NEW.created_by_user IS NULL OR NEW.created_by_user = '' THEN
SET NEW.created_by_user = #users_id;
END IF;
IF NEW.modified_by_user IS NULL OR NEW.modified_by_user = '' THEN
SET NEW.modified_by_user = #users_id;
END IF;
END$$
CREATE TRIGGER tg_notes_upd BEFORE UPDATE ON notes FOR EACH ROW
BEGIN
IF NEW.modified_by_user = OLD.modified_by_user THEN
SET NEW.modified_by_user = #users_id;
END IF;
END$$
I am not claiming this is how to do it, but it seems to work, and would appreciate any opinions. Thanks
DELIMITER $$
CREATE PROCEDURE `createRecord` ()
BEGIN
IF NEW.created_by_user IS NULL OR NEW.created_by_user = '' THEN
SET NEW.created_by_user = #users_id;
END IF;
IF NEW.modified_by_user IS NULL OR NEW.modified_by_user = '' THEN
SET NEW.modified_by_user = #users_id;
END IF;
END$$
CREATE PROCEDURE `modifyRecord` ()
BEGIN
IF NEW.modified_by_user = OLD.modified_by_user THEN
SET NEW.modified_by_user = #users_id;
END IF;
END$$
CREATE TRIGGER tg_notes_upd BEFORE UPDATE ON notes FOR EACH ROW
BEGIN
CALL createRecord();
END$$
CREATE TRIGGER tg_notes_ins BEFORE INSERT ON notes FOR EACH ROW
BEGIN
CALL modifyRecord();
END$$
DELIMITER ;
Related
CREATE TRIGGER UPDATE_CAR_DETALS
AFTER UPDATE ON BOOKING_DETALS
FOR EACH ROW
WHEN (IFNULL(TO_CHAR(NEW.ACT_RET_DT_TIME),NULL) <> 'NULL' OR
NEW.BOOKING_STATUS ='C')
Multi statements in triggers need a BEGIN and END
Also when you use query tabs you need also DELIMITER
CASE WHEN you would use if you have multiple choices, but you use better IF THEN
DELIMITER $$
CREATE TRIGGER UPDATE_CAR_DETALS
AFTER UPDATE ON BOOKING_DETALS
FOR EACH ROW
BEGIN
IF (IFNULL(TO_CHAR(NEW.ACT_RET_DT_TIME),NULL) <> 'NULL' OR NEW.BOOKING_STATUS ='C') then
BEGIN
IF NEW.BOOKING_STATUS ='C' THEN
UPDATE CAR SET AVAILABILITY_FLAG = 'A' , LOC_ID = NEW.PICKUP_LOC
WHERE REGISTRATION_NUMBER = NEW.REG_NUM;
ELSE
IF IFNULL(TO_CHAR(NEW.ACT_RET_DT_TIME),NULL) <> NULL THEN
UPDATE CAR SET AVAILABILITY_FLAG = 'A' , LOC_ID = NEW.DROP_LOC, MILEAGE = MILEAGE+GET_MILEAGE
WHERE REGISTRATION_NUMBER = NEW.REG_NUM;
END IF;
END IF;
END;
END IF;
END;
DELIMITER ;
I want to execute the trigger as long as my first condition is met. But it gives me an error when creating it
DELIMITER $$
CREATE TRIGGER `actualizar`
AFTER UPDATE ON `detalle_ventas`
FOR EACH ROW
BEGIN
IF NEW.estado = 'Cancelado' then
UPDATE articulos SET stock += NEW.cantidad
WHERE articulos.id = NEW.idarticulo
AND articulos.inventariable = 1;
END IF;
END$$
DELIMITER ;
You have to surrond the update with the IF clause if it is canceled
DELIMITER $$
CREATE TRIGGER `actualizar`
AFTER UPDATE ON `detalle_ventas`
FOR EACH ROW
BEGIN
IF NEW.estado = 'Cancelado' then
UPDATE articulos SET stock = stock + NEW.cantidad
WHERE articulos.id = NEW.idarticulo
AND articulos.inventariable = 1;
END IF;
END$$
DELIMITER ;
I am trying to write a trigger where if an incoming value is empty (in other words ''), then insert NULL in the table. I have :
DELIMITER //
CREATE TRIGGER avoid_empty
BEFORE INSERT ON EVALUATION
FOR EACH ROW
BEGIN
IF mark = '' THEN SET NEW.mark = NULL;
END IF;
END;
//
DELIMITER ;
Which executes without errors, but it doesn't do what I need.
Try:
DELIMITER //
CREATE TRIGGER `avoid_empty` BEFORE INSERT ON `EVALUATION`
FOR EACH ROW
BEGIN
IF NEW.`mark` = '' THEN
SET NEW.`mark` := NULL;
END IF;
END//
DELIMITER ;
See db-fiddle.
I am trying to create a trigger to prevent empty string insert for cname column and values smaller than 7000 and larger than 8000 for empno column by making it null. Here is how I have done it:
delimiter $$
CREATE TRIGGER test1
BEFORE insert
ON clients
FOR EACH row BEGIN
if new.cname = '' THEN
SET new.cname = null;
if new.empno <7000 THEN
SET new.empno = null;
if new.empno>8000 THEN
SET new.empno = null;
end if;
end if;
end if;
end;
The cname column works fine. But the empno will accept anything and I cannot figure why. My table is something like this:
CREATE TABLE clients
(
empno INTEGER NOT NULL DEFAULT 7654
cname VARCHAR(20) NOT NULL
);
You should set each end if after the corresponding if:
delimiter $$
CREATE TRIGGER test1
BEFORE insert
ON clients
FOR EACH row BEGIN
if new.cname = '' THEN
SET new.cname = null;
end if;
if new.empno <7000 THEN
SET new.empno = null;
end if;
if new.empno>8000 THEN
SET new.empno = null;
end if;
end;
Please tell me whats wrong with the code. Not able to fine the bug
DELIMITER $$
CREATE TRIGGER update_status BEFORE Update ON listing_basic_new_updated
FOR EACH ROW
if new.processing_status is not null
then begin
SET new.rep_status = New.processing_status;
end;
elseif new.televeri_status is not null
then begin
SET new.rep_status = New.televeri_status;
end;
elseif new.verification_status is not null
then begin
SET new.rep_status = New.verification_status;
end;
end if;
END$$
DELIMITER ;
I think you're missing a BEGIN that would match up to your END at the end:
DELIMITER $$
CREATE TRIGGER update_status BEFORE Update ON listing_basic_new_updated
FOR EACH ROW
BEGIN
if new.processing_status is not null
then begin
SET new.rep_status = New.processing_status;
end;
elseif new.televeri_status is not null
then begin
SET new.rep_status = New.televeri_status;
end;
elseif new.verification_status is not null
then begin
SET new.rep_status = New.verification_status;
end;
end if;
END$$
DELIMITER ;
I think you might be able to replace all of it with
SET new.rep_status = COALESCE(new.processing_status, new.televeri_status,
new.verification_status, new.rep_status);
COALESCE: "Returns the first non-NULL value in the list, or NULL if there are no non-NULL values."