0
我有一個表Initial_Fees
。這張表中有一列叫做Initial_Consult_Fee
,每年不能超過30,000。下面是我的桌子。變量在MySQL中計算後變爲NULL觸發器
CREATE TABLE `initial_fees` (
`idInitial_Fees` int(11) NOT NULL AUTO_INCREMENT,
`idPortfolio` int(11) NOT NULL,
`Current_Time_Stamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`Initial_Gross_Fee` double NOT NULL,
`Initial_Consult_Fee` double NOT NULL,
`Updated_Date` date NOT NULL,
`idTransactions` int(11) DEFAULT NULL,
PRIMARY KEY (`idInitial_Fees`)
)
所以,我創建了一個觸發器,會做檢查,Initial_Consult_Fee
是否高於30,000或不是。除此之外,它會圍繞所有的價值。
DELIMITER $$
CREATE TRIGGER `Initial_Fees_BINS` BEFORE INSERT ON `Initial_Fees` FOR EACH ROW
begin
DECLARE `initial` DOUBLE;
DECLARE `ongoing` DOUBLE;
DECLARE `total` DOUBLE;
DECLARE `valueToBeEntered` DOUBLE;
SET `initial`:=(SELECT SUM(`Initial_Consult_Fee`) FROM `Initial_Fees` WHERE `Updated_Date` BETWEEN MAKEDATE(YEAR(New.Updated_Date),1) AND MAKEDATE(YEAR(New.Updated_Date),365));
SET `ongoing`:=(SELECT SUM(`Ongoing_Consult_Fee`) FROM `Ongoing_Fees` WHERE `Updated_Date` BETWEEN MAKEDATE(YEAR(New.Updated_Date),1) AND MAKEDATE(YEAR(New.Updated_Date),365));
SET `total` := `initial`+`ongoing`;
IF((New.Initial_Consult_Fee+`total`) > 30000) THEN
SET `valueToBeEntered`:= 30000 - `total`;
END IF;
IF((New.Initial_Consult_Fee+`total`) < 30000) THEN
SET `valueToBeEntered`:= New.Initial_Consult_Fee;
END IF;
SET New.Initial_Gross_Fee = ROUND(New.Initial_Gross_Fee,2),
New.Initial_Consult_Fee = ROUND(`valueToBeEntered`,2);
END;
然而,當我輸入數據,如果New.Initial_Vision_Fee+total
我得到下面的錯誤
Column 'Initial_Consult_Fee' cannot be null
什麼是錯我的代碼?
hmm..It仍說同樣的事情.. – 2014-10-10 08:28:48
你好................? ...... – 2014-10-10 08:49:40
@Sniper。 。 。你說什麼似乎不太可能。答案中的解決方案應該保證'total'永遠不是'NULL',並且通過閱讀代碼,除非'New.Initial_Consult_Fee'爲NULL,否則這是將值設置爲'NULL'的唯一方法。在這種情況下,將新值設置爲'coalesce(New.Initial_Consult_Fee,0)'。 – 2014-10-12 21:42:21