【问题标题】:How to declare a value in a trigger in mysql?如何在mysql的触发器中声明一个值?
【发布时间】:2014-05-04 22:34:31
【问题描述】:

我正在尝试使用 mysql 和 phpmyadmin 为我的数据库编写触发器。我有一个数据库,其中有一个客户信息表,其中存储了信用卡号,还有一个酒店预订表,其中存储了客户的付款方式。我希望我的触发器在更新客户信息后激活,如果信用卡号被删除以更改付款方式。问题是phpmyadmin没有编译。

CREATE TRIGGER after_credit_card_update
AFTER UPDATE  
ON client
for each row
BEGIN
       declare cl_id int;
       IF ( (new.Credit_Card_Number = " ") or (new.Credit_Card_Number is null)) then
select into cl_id
                select reservation.Client_Id from 
reservation where ((reservation.Client_Id = new.Client_Id) and (reservation.Payment = 'Credit')) 
limit 1 ;
    update reservation
    set reservation.Payment = "NOT PAID"
    where reservation.Client_Id = cl_id;
END IF;
END$$

我认为问题出在我声明我的价值时:cl_id

1064 - 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 8 行的“进入 cl_id select reservation.Client_Id from reservation where”附近使用正确的语法

已修复:

DELIMITER $$
CREATE TRIGGER after_credit_card_update
AFTER UPDATE  
ON client
for each row
BEGIN
declare cl_id int;
IF ( (new.Credit_Card_Number = " ") or 
new.Credit_Card_Number is null)) then
 select reservation.Client_Id into cl_id from reservation 
here ((reservation.Client_Id = new.Client_Id) and 
reservation.Payment = 'credit')) 
limit 1 ;
update reservation
set reservation.Payment = "NOT PAID"
where reservation.Client_Id = cl_id;
END IF;
END$$

分隔符;

【问题讨论】:

  • 您在尝试创建过程之前是否将分隔符设置为$$
  • 是的,我什么也没做

标签: mysql triggers declare


【解决方案1】:

这里不做详细介绍,但是您需要在触发器之前和之后添加 delimiter 语句 - 这样 MySQL 就会知道如何解析代码。例如:

DELIMITER $$

CREATE TRIGGER after_credit_card_update
AFTER UPDATE  
ON client
for each row
BEGIN
       declare cl_id int;
       IF ( (new.Credit_Card_Number = " ") or 
new.Credit_Card_Number is null)) then
select into cl_id
                select reservation.Client_Id from 
reservation where reservation.Client_Id = new.Client_Id 
limit 1 ;
    update reservation
    set reservation.Payment = "NOT PAID"
    where reservation.Client_Id = cl_id;
END IF;
END$$

DELIMITER ;

【讨论】:

  • 我复制粘贴您的解决方案,但仍然无法正常工作
猜你喜欢
  • 2021-06-23
  • 1970-01-01
  • 1970-01-01
  • 2016-12-16
  • 1970-01-01
  • 1970-01-01
  • 2013-09-02
  • 2014-12-20
  • 2017-03-09
相关资源
最近更新 更多