请查看下面的SQL查询。
DELIMITER $$
CREATE TRIGGER `Portfolio_AINS` AFTER INSERT ON `Portfolio` FOR EACH ROW
INSERT INTO Initial_Fees (idPortfolio, Current_Time_Stamp, Initial_Gross_Fee, Initial_Monetary_Fee)
VALUES (
New.idPortfolio,
current_timestamp,
(New.Invest_Amount*(New.Initial_Gross_Fee/100)),
(New.Invest_Amount*(New.Initial_Gross_Fee/100))*(New.Initial_Company_Fee/100)
)
在这里,Initial_Monetary_Fee = Initial_Gross_Fee * (New.Initial_Company_Fee/100)
但我写了(New.Invest_Amount*(New.Initial_Gross_Fee/100))*(New.Initial_Company_Fee/100)
而不是那样,我想创建变量并为它们赋值,这样我就可以避免这么长的计算。举个例子,如下所示。
InitialGrossFee = (New.Invest_Amount*(New.Initial_Gross_Fee/100))
IntialMonetaryFee = InitialGrossFee * (New.Initial_Company_Fee/100)
那么,如何在MySQL MySQL触发器中创建这样的变量并存储值?我也希望得到一些解释。
答案 0 :(得分:0)
如果在触发器中需要多个语句,则必须使用BEGIN .... END。 然后您可以照常为uservars分配值:
CREATE TRIGGER `Portfolio_AINS` AFTER INSERT ON `Portfolio`
FOR EACH ROW
BEGIN
SET @grossfee := New.Invest_Amount*(New.Initial_Gross_Fee/100);
INSERT INTO Initial_Fees (idPortfolio, Current_Time_Stamp, Initial_Gross_Fee, Initial_Monetary_Fee)
VALUES (
New.idPortfolio,
current_timestamp,
@grossfee,
@grossfee *(New.Initial_Company_Fee/100)
);
END$$