我想要一种在语句或视图为真时更新表的方法。触发器和存储过程的问题在于它们依赖于用户操作,即编辑,删除或更新。我需要的是一个表,只要给定的语句根据系统日期变为真实,就会被更新。实现如下:
查看
CREATE VIEW `vw_dueusers` AS
select UserID,Penalty from indextbl
where duedate>now() and balance>0;
程序
DELIMITER $$
CREATE PROCEDURE sp_PenaltyInsert(
in vUserID int,
in vPenalty text
)
-- Wonder how to get the variables from the View above for my INs
begin
INSERT INTO `penaltytbl`
-- The idea is to keep updating this table whenever when user doesnt pay on agreed date
(
UserID,
Penalty
)
select UserID,Penalty from indextbl
where duedate>now() and balance>0;
-- or
--select * from vw_dueusers;
-- How to avoid duplicate entries?
end
TRIGGER ????想知道我是否创建得很好
DELIMITER $$
CREATE TRIGGER Tr_PenaltyInsert BEFORE INSERT ON indextbl
-- the idea is automation and not relying on the Insert Action. Once the view above becomes true
-- the balance doesnt become due, due to an insert but when user doesnt pay on agreed date
FOR EACH ROW
BEGIN
Declare vUserID int;
Declare vPenalty int;
set new.vUserID =(select UserID from indextbl where duedate>now() and balance>0);
set new.vPenalty =(select Penalty from indextbl where duedate>now() and balance>0);
INSERT INTO `penaltytbl`
(
UserID,
Penalty
)
values
(
vUserID,
vPenalty
);
/*values
(
New.vUserID,
New.vPenalty
);*/
-- How to avoid duplicate entries?
END;
事件?