I have written a small trigger function in MySQL . This is the trigger query i have written and the condition are not checked direct default value of created_id is assigned.
CREATE TRIGGER `complain_attend_log` AFTER INSERT ON `attend_complain`
FOR EACH ROW BEGIN
DECLARE attend_created INT;
DECLARE created_id INT default 5;
SET attend_created = (SELECT attend_created FROM allotments WHERE complain_id = NEW.complain_id);
IF(attend_created = 1) THEN
SET created_id = 6;
END IF;
INSERT INTO complain_logs
VALUES(null,NEW.complain_id,NEW.allotment_id,NEW.attend_complain_id,NEW.is_closed,created_id,1,now(),1,now());
END
Related
I have created a Trigger after INSERT on the table AcademicYearTermLevel which uses a cursor value fetched from another query. I want to use each cursor value in a while loop and insert rows into table SubjectYearTermLevel.
DROP TRIGGER IF EXISTS after_academicyearterm_insert
DELIMITER $$
CREATE TRIGGER after_academicyearterm_insert
AFTER INSERT
ON AcademicYearTerm
FOR EACH ROW
BEGIN
DECLARE temp INT;
DECLARE subj_done, form_done BOOLEAN DEFAULT FALSE;
DECLARE s_id INT;
DECLARE curSubject CURSOR FOR SELECT subject_id FROM Subject;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET subj_done = TRUE;
SET temp = 1;
OPEN curSubject;
cur_subject_loop: LOOP
FETCH FROM curSubject INTO s_id;
IF subj_done THEN
CLOSE curSubject;
LEAVE cur_subject_loop;
END IF;
WHILE temp <= 6 DO
INSERT INTO SubjectYearTermLevel VALUES (NULL, s_id, NEW.yearTerm_id, temp);
SET temp = temp + 1;
END WHILE
END LOOP cur_subject_loop;
END$$
DELIMITER ;
However the problem is that the cursor only seems to fetch one value from the SELECT query
Silly mistake, I SET temp = 1; outside the FETCH, and hence after the first cursor value, the while loop never ran.
-- SET temp = 1;
OPEN curSubject;
cur_subject_loop: LOOP
FETCH FROM curSubject INTO s_id;
SET temp = 1;
IF subj_done THEN
CLOSE curSubject;
LEAVE cur_subject_loop;
END IF;
1_to_6_temp: WHILE temp <= 6 DO
INSERT INTO SubjectYearTermLevel VALUES (NULL, s_id, NEW.yearTerm_id, temp, -1);
SET temp = temp + 1;
END WHILE 1_to_6_temp;
-- CLOSE curSubject;
END LOOP cur_subject_loop;
I have to use a trigger that will update the inserted row. Following is my trigger.
DELIMITER $$
USE `kikan_db`$$
CREATE TRIGGER `t_trigger` AFTER INSERT ON `t_order`
FOR EACH ROW BEGIN
Declare kb_rate int;
Declare kb_amount int;
REPLACE INTO t_order_sales_month
SET order_no = NEW.order_no,
search_sales_month = getSalesMonth(NEW.order_no);
SELECT kickback_rate INTO kb_rate from m_customer where customer_code = New.bill_customer_code;
SET kb_amount := 9999 / kb_rate;
UPDATE `t_order` SET `t_order`.kickback_amount = kb_amount where `t_order`.order_no = NEW.order_no;
END;
$$
DELIMITER ;
Getting error at update.
It is mandatory for me to use AFTER instead of BEFORE type of trigger
Instead of using only after insert/update trigger, (1) you can change the value of virtual new row on before insert/update :
Declare kb_rate int;
Declare kb_amount int;
SELECT kickback_rate INTO kb_rate from m_customer
where customer_code = New.bill_customer_code;
SET kb_amount := 9999 / kb_rate;
SET new.kickback_amount = kb_amount;
(2)And execute the other SQL statements in a after insert/update trigger:
REPLACE INTO t_order_sales_month
SET order_no = NEW.order_no,
search_sales_month = getSalesMonth(NEW.order_no);
The code I have doesn't work, I wish to insert data from another table using trigger. Before it inserted, I wish to checking the value is null or not. But when I execute the trigger, it doesn't insert any data.
Here's my code :
DELIMITER $$
CREATE TRIGGER `filterNoHP`
BEFORE INSERT ON smsra_sms FOR EACH ROW
BEGIN
DECLARE IDx int(11);
DECLARE NumberHP varchar(20);
DECLARE WaktuKirim timestamp;
DECLARE IsiSMS text;
SELECT ID, SenderNumber, ReceivingDateTime,
TextDecoded into IDx, NumberHP, WaktuKirim, IsiSMS FROM ebsms.inbox
WHERE NumberHP in (SELECT NoHP FROM smsra_blokir_number)
and DATE(ReceivingDateTime) = curdate();
IF NumberHP = null then
insert into smsra_sms
set idsms = IDx, SendingDateTime = WaktuKirim, isi = IsiSMS, pengirim = NumberHP
ON duplicate key update idsms = idsms + IDx;
ELSE
insert into smsra_trash
set Nohp = NumberHP
ON duplicate key update Nohp = Nohp + NumberHP;
END IF;
END$$
DELIMITER ;
any idea what's wrong?
Please help, Thank you
This is the database vehicle table's trigger
DROP TRIGGER IF EXISTS InsertVehTrig;
DELIMITER $$
CREATE TRIGGER InsertVehTrig AFTER INSERT
ON Vehicle FOR EACH ROW
SWL_return:
BEGIN
DECLARE Cph CHAR(50);
DECLARE DevID CHAR(12);
DECLARE VehID BIGINT;
DECLARE TmpID BIGINT;
DECLARE DevCount INT;
SET Cph = rtrim(ltrim(NEW.cph));
SET VehID = NEW.ID;
SET DevID = NEW.DevID;
if(VehID is null) then
select count(id) into #DevCount from vehicle where (cph=#Cph) or (DevID=#DevID);
-- 条件:当前的车牌号 或 设备ID
end if;
if (DevCount > 1) then -- 如果记录数,超过1,则认为有重复
-- Rollback not supported in trigger
SET #SWV_Null_Var = 0;
Leave SWL_return;
else
if (DevCount = 1) then
select ID INTO #TmpID from Vehicle where (Vehicle.cph = #Cph) or (Vehicle.DevID = #DevID);
if (TmpID != VehID) then -- --如果增加的车牌号码与数据库中的在相同的,则不允许增加
-- Rollback not supported in trigger
Leave SWL_return;
SET #SWV_Null_Var = 0;
end if;
end if;
end if;
update vehicle set cph = #Cph where ID = #VehID;
END;
Right now i m trying to insert new data row in the vehicle table, but error with this
ERROR 1442: Can't update table 'vehicle' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
SQL Statement:
INSERT INTO `gis_server`.`vehicle` (`TrackerNum`, `cph`, `DevID`, `DevType`) VALUES ('1', 'NR09B00555', 'NR09B00555', '2')
those database are designed by 3 party company,
How do i insert data to vehicle table?
You can achieve this by making two changes to your approach:
Firstly, use a BEFORE trigger rather than an AFTER
Secondly, rather than updating the same table, update the NEW table, setting the column value to the new value before the NEW table hits the table targeted by the INSERT.
For example, replace your UPDATE line with the following:
UPDATE NEW SET cph = Cph;
MySQL doesn't allow you to edit the data in the table on which a trigger is created in that trigger, but you can edit the NEW table to modify the values going into that table.
DELIMITER //
DROP PROCEDURE IF EXISTS sp_vk_suspend//
CREATE PROCEDURE sp_vk_suspend2()
declare today = DATETIME DEFAULT NULL;
declare accid = INT DEFAULT 11;
set today = now();
BEGIN
SELECT * FROM members where expirydate < today;
END //
This is my stored procedure. I have to add one or more query depend with id from members table. How can I retrieve id from members table.
ex: I have to add this query in my sp
UPDATE members
SET suspend = 1
WHERE id = 'some id from mem: tables'
i see IMHO cursors it's what you need
DECLARE done INT DEFAULT FALSE;
DECLARE cur1 CURSOR FOR SELECT id FROM members where expirydate < today;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
DECLARE a INT;
OPEN cur1;
my_loop: LOOP
FETCH cur1 INTO a
IF done THEN
LEAVE my_loop;
END IF;
UPDATE members SET suspend=1 WHERE id = a;
INSERT INTO another(member_id) values(a);
END LOOP;
CLOSE cur1;