所以我有这些表:数据库触发器和计算
CREATE TABLE `chittytransactions` (
`ChittyTransactionID` int(11) NOT NULL,
`AuctionID` int(11) NOT NULL,
`ChittyAccNo` int(11) DEFAULT NULL,
`Date` datetime DEFAULT NULL,
`Amount` double DEFAULT NULL,
`Description` varchar(50) DEFAULT NULL,
`TransRefence` varchar(50) DEFAULT NULL COMMENT 'Reference from actual Bank transaction',
`TransStatus` tinyint(1) DEFAULT NULL COMMENT 'If Transaction Pending or Cleared',
`ClearanceDate` datetime DEFAULT NULL,
`PaymentMethod` int(1) DEFAULT NULL COMMENT '0- Cash, 1- bank transfer, 2- personal credit etc'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `chittyusers` (
`ChittyAccNo` int(11) NOT NULL,
`UserId` int(11) NOT NULL,
`ChittyID` int(11) NOT NULL,
`LatePaymentFee` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `users` (
`UserId` int(11) NOT NULL,
`UserName` varchar(45) NOT NULL,
`UserNameVerified` tinyint(1) DEFAULT '0',
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
什么即时试图做的是检查,如果交易日期是一样的,在chittytransaction
表中清除日期,如果不加一晚支付费用为chittyusers
表(最好根据日期计算,根据日期计算延迟支付的额外费用)。这应该在插入之前完成,我正在使用触发器。到目前为止,我有这样的:
ROP TRIGGER IF EXISTS chitty_beforet_trig;
DELIMITER ;;
CREATE TRIGGER chitty_beforet_trig BEFORE Insert ON chittytransaction
FOR EACH ROW
BEGIN
DECLARE `mainDate` datetime;
DECLARE `claredDate` datetime;
DECLARE `chitAccNo` INT(11);
DECLARE `userId` INT(11);
DECLARE `latePay` DOUBLE;
DECLARE late TINYINT;
DECLARE cursor1 CURSOR FOR SELECT ChittyAccNo FROM accounting.`chittyusers`;
DECLARE cursor2 CURSOR FOR SELECT `Date` FROM accounting.`chittyTransaction`;
DECLARE cursor3 CURSOR FOR SELECT ClearanceDate FROM accounting.`chittyTransaction`;
DECLARE cursor4 CURSOR FOR SELECT UserId FROM accounting.`users`;
OPEN cursor1;
OPEN cursor2;
OPEN cursor3;
OPEN cursor4;
FETCH cursor1 INTO chitAccNo;
FETCH cursor2 INTO mainDate
FETCH cursor3 INTO claredDate;
FETCH cursor3 INTO userId;
CASE
WHEN claredDated <> mainDate THEN
SET late ='1';
ELSE
SET late = '0';
END;
END CASE;
IF late THEN
UPDATE `chittyusers` SET LatePaymentFee = 50 WHERE UserId = userId;
END IF;
CLOSE cursor1;
CLOSE cursor2;
CLOSE cursor3;
CLOSE cursor4;
END;;
DELIMITER;
我不断收到许多不同的错误等的广告,我不知道如果我这样做是正确与否。以前从未使用过,所以它有点困难。任何人都可以告诉我,我做错了请和任何解决方案,将不胜感激。
谢谢。多亏了你。但为什么使用案例会出现问题? – luffy
没有问题;它只是没有必要。报废它会减少变量,代码行和复杂性。 –
我使用它错了吗?如果是的话,我会如何改变它? – luffy