Multiple IF in Mysql Trigger is not working - php

I'm trying to log changes of a table into another table. But my trigger is not working. Need help:
// Mysql code below
create trigger upd_stu_info
before update
on kisalg_student
for each row
begin
IF OLD.stu_name != NEW.stu_name
THEN
INSERT INTO `kisalg_logactions`
(adm_no, change_type,change_head, old_value,new_value,user,date_time,session)
VALUES
(NEW.adm_no, 'stu_info','Change In Student Name ', OLD.stu_name, NEW.stu_name, NEW.user, NEW.date_time, NEW.session);
END IF;
IF OLD.fat_name != NEW.fat_name
THEN
INSERT INTO `kisalg_logactions`
(adm_no, change_type, change_head, old_value, new_value, user, date_time, session)
VALUES
(NEW.adm_no, 'stu_info','Change In Father Name ', OLD.fat_name, NEW.fat_name, NEW.user, NEW.date_time, NEW.session);
END IF;
end;

I just tried the trigger on mysql and using the following I did not get any syntax error
delimiter //
create trigger upd_stu_info
before update
on kisalg_student
for each row
begin
IF OLD.stu_name != NEW.stu_name
THEN
INSERT INTO `kisalg_logactions`
(adm_no, change_type,change_head, old_value,new_value,user,date_time,session)
VALUES
(NEW.adm_no, 'stu_info','Change In Student Name ', OLD.stu_name, NEW.stu_name, NEW.user, NEW.date_time, NEW.session);
END IF;
IF OLD.fat_name != NEW.fat_name
THEN
INSERT INTO `kisalg_logactions`
(adm_no, change_type, change_head, old_value, new_value, `user`, date_time, session)
VALUES
(NEW.adm_no, 'stu_info','Change In Father Name ', OLD.fat_name, NEW.fat_name, NEW.`user`, NEW.date_time, NEW.session);
END IF;
end; //

Related

INSERT INTO with no SELECT throws: Error #1242: Subquery return more than 1 row

I am trying to insert data into my Database but my query throws the error #1242: Subquery returns more than one row
The table does get inserted 1 or 2 rows but then it throws this error.
Here is my query:
INSERT INTO candidato (nombres, apellidos, nombrePuesto, ciudadOrigen, ubicacionPuesto, reubicacion, empleadoActual, certificaciones, disponibilidad, sueldoActual, nivelIngles, beneficiosActuales, observacionesGen, expectativaSalarial, experienciaGen, actitud, expectativasPuesto, añosExperiencia, solicitante, staffingPlan, cubreCapacidades, observacionesTec, tipoCandidato)
VALUES ($nombres, $apellidos, $nombrePuesto, $ciudadOrigen, $ubicacionPuesto, $reubicacion, $empleadoActual, $certificaciones, $disponibilidad, $sueldoActual, $nivelIngles, $beneficiosActuales, $observacionesGen, $expectativaSalarial, $experienciaGen, $actitud, $expectativasPuesto, $añosExperiencia, $solicitante, $staffingPlan, $cubreCapacidades, $observacionesTec, $tipoCandidato);
I am also using a trigger that inserts data in another table when this one gets a new row inserted:
Here is the trigger:
DELIMITER $$
CREATE TRIGGER asignarNotificacionCandidatoPropuesto
AFTER INSERT ON CANDIDATO
FOR EACH ROW
BEGIN
SET #idSolicitud = (SELECT DISTINCT r.idSolicitud FROM REQUISICION AS r INNER JOIN STAFFING_PLAN AS s ON r.idRequisicion=s.requisicion INNER JOIN CANDIDATO AS c ON c.staffingPlan=NEW.staffingPlan);
INSERT INTO NOTIFICACION(fechaHora, tipo, contenido)
VALUES (NOW(), 'candidatoPropuesto', CONCAT("{", "'idCandidato' : '", NEW.idCandidato, "'}"));
-- ", ", "'idSolicitud' : '", #idSolicitud,
IF NEW.idCandidato IS NOT NULL THEN
BEGIN
INSERT INTO RECIBE(destinatario, notificacion)
VALUES(NEW.solicitante, (SELECT MAX(idNotificacion) FROM NOTIFICACION));
END;
END IF;
END $$
DELIMITER ;

MySql insert Trigger Not working

I have written a before insert trigger that will not insert the data in the tables if the 'ratings' > 5 and inserts it on a table 'rating_check' with a message .For 'ratings' <= 5 the data is inserted on all the tables.But its not working nor am I getting any errors. Here is the trigger code-
DELIMITER $$
CREATE
TRIGGER `mytrigger` BEFORE INSERT
ON `review`
FOR EACH ROW BEGIN
IF NEW.ratings > 5 THEN
SET #trigger_desc = 'You cannot rate greater than 5!';
INSERT INTO `rating_check` (`trig_id`, `trigger_desc`) VALUES ('', #trigger_desc);
ELSE
INSERT INTO `developer_details` (`developer_id`,`developer_name`,`developer_email`) VALUES ('$appDevId','$appDevName','$appDevEmail');
INSERT INTO `apps` (`app_id`,`app_name`,`app_icon`,`dev_id`) VALUES (LAST_INSERT_ID(),'$appName','$appIcon','$appDevId');
INSERT INTO `app_details` (`app_id`,`description`,`version`,`release_date`,`size`) VALUES (LAST_INSERT_ID(),'$appDesc','$appVersion','$appDate','$appSize');
INSERT INTO `app_specs` (`app_id`,`downloads`,`category_name`) VALUES (LAST_INSERT_ID(),'$appDownloads','$appCategory');
INSERT INTO `review` (`app_id`,`ratings`) VALUES (LAST_INSERT_ID(),'$appRatings');
END IF;
END$$
'trig_id' is auto incremented here.Please help me with this!

How to write multiple queries in stored procedure

What I'm trying to do is when user roles changed to 1 or from 1 then delete all his entries from ForumManager table which IsDirect value is 0 or if its value changes to 1 then I need to insert person's entry to ForumManager table for all ForumID.
But for inserting it I have to run 1 more queries to get ForumID and insert in ForumManager with loop.
What I try till now is incomplete and maybe not correct or right way I don't know how to run loop inside stored procedure.
delimiter //
CREATE PROCEDURE update_forum_admin (IN user_id INT, IN previous_role INT,IN new_role INT)
BEGIN
if(previous_role == 1)
{
DELETE ForumManager WHERE UserID=user_id AND IsDirect=0
}
elseif(new_role == 1)
{
SELECT ForumID FROM Forum
INSERT ForumManager (ForumID,UserID,IsDirect) VALUES (forum_id,user_id,0)
}
END//
delimiter ;
If you have a column name user_id in Forum table than rename the parameter from user_id to userId.
delimiter //
CREATE PROCEDURE update_forum_admin (IN userId INT, IN previous_role INT,IN new_role INT)
BEGIN
if(previous_role == 1)
{
DELETE ForumManager WHERE UserID=userId AND IsDirect=0
}
elseif(new_role == 1)
{
INSERT ForumManager (ForumID, UserID, IsDirect)
SELECT ForumID, userId, 0 FROM Forum group by ForumID, userId, 0
}
END//
delimiter ;
You can declare a coursor with ID_forum and iterate it inside an elseif statement like below(is a pseudo code, if you have e problem to implement it let me know):
CREATE PROCEDURE update_forum_admin (IN user_id INT, IN previous_role INT,IN new_role INT)
BEGIN
CURSOR CUR_ID_FORUM
IS
SELECT ForumID FROM Forum;
if(previous_role == 1)
{
DELETE ForumManager WHERE UserID=user_id AND IsDirect=0
}
elseif(new_role == 1)
{
FOR REC_ID_FORUM IN CUR_ID_FORUM
LOOP
INSERT ForumManager (ForumID,UserID,IsDirect) VALUES (REC_ID_FORUM,user_id,0)
END LOOP;
}
END update_forum_admin ;
its quiet late but Mohit you can get rid of syntax error by modifying code like
CREATE PROCEDURE update_forum_admin (IN user_id INT, IN previous_role INT,IN new_role INT)
BEGIN
if(previous_role = 1)
BEGIN
DELETE ForumManager WHERE UserID=user_id AND IsDirect=0
END
if(new_role = 1)
BEGIN
SELECT ForumID FROM Forum
INSERT ForumManager (ForumID,UserID,IsDirect) VALUES (forum_id,user_id,0)
END
Also I Can see when you delete a user from ForumManager you do not delete for particular Forum. May be you have only one Forum and if that is the case then take top 1 Forum Id from Forum table

trigger after insert on table 1, insert on table 2 with where condition column from table 1

My trigger is not working at all.
DELIMITER $$
CREATE TRIGGER leave_credit_nt
AFTER INSERT ON employees
FOR EACH ROW
BEGIN
INSERT INTO nonteaching_leavecredit (IdNum, Date_Hired, Emp_Stat, Date_Leave_Ac, Vac_Leave, Sick_Leave)
VALUES (new.IdNum, new.Date_Hired, new.Emp_Stat, new.Date_Hired + INTERVAL 1 YEAR, 0, 0)
WHERE employees.Emp_Type = 'NON-TEACHING';
END $$
DELIMITER
I want that the trigger will insert values into my second table only those with employees.emp_type = 'NON-TEACHING'. Can someone help me get this trigger working?
emp_type is a column in my table employees.
Your INSERT query is wrong. MySQL INSERT Syntax does not support the WHERE clause so your query as it stands will fail.
You cant use WHERE condition with INSERT query. If you are using WHERE condition, it means you already have that row in the table. So , you should use UPDATE query instead of INSERT.
You can add conditional logic to your trigger with:
if new.Emp_Type = 'NON-TEACHING' then
INSERT INTO nonteaching_leavecredit (IdNum, Date_Hired, Emp_Stat, Date_Leave_Ac, Vac_Leave, Sick_Leave)
VALUES (new.IdNum, new.Date_Hired, new.Emp_Stat, new.Date_Hired + INTERVAL 1 YEAR, 0, 0);
end if;

MySQL trigger syntax error on insert after with variables

I am trying to track users sales based on the new transactions that come through. The transactions are assigned through our site and not their desktop application. It is giving me a syntax error, but I can't seem to figure out why.
This is my code:
DROP TRIGGER IF EXISTS AssignTrans;
DELIMITER $$
CREATE TRIGGER `testing_cake3`.`AssignTrans` AFTER INSERT ON `main_1` FOR EACH ROW
BEGIN
SET #t1 = (SELECT team_id FROM team_assignments WHERE team_assignments.misc_id = NEW.CUSTOMER_ID AND team_assignments.type = 1);
SET #t2 = (SELECT team_id FROM team_assignments WHERE team_assignments.misc_id = NEW.GROUP_ID AND team_assignments.type = 0);
IF (#t1) THEN
INSERT INTO team_trans (team_id, trans_id, type, misc_id) VALUES (#t1, NEW.id, 0, 1 );
ELSE IF (#t2) THEN
INSERT INTO team_trans (team_id, trans_id, type, misc_id) VALUES (#t2, NEW.id, 0, 1 );
END IF;
END $$
DELIMITER ;
THe answer is CHANGING THE ELSE IF TO ElSEIF

Categories