USE brendasdms;
DROP TRIGGER IF EXISTS Customers_Before_Insert;
DELIMITER //
CREATE TRIGGER Customers_Before_Insert
BEFORE INSERT ON Customers
FOR EACH ROW
BEGIN
IF(NEW.Email NOT LIKE '%_@%_.__%') THEN
SIGNAL SQLSTATE VALUE '45000'
SET MESSAGE_TEXT= 'The email column doesn\'t contain @ symbol or .(dot) domain';
END IF ;
IF (NEW.Phone NOT REGEXP '[0-9]{3}-[0-9]{3}-[0-9]{4}') THEN
SIGNAL SQLSTATE VALUE '45000'
SET MESSAGE_TEXT= 'The Phone Number Needs to be at least 10 digits long';
END IF ;
END; //
DELIMITER ;
INSERT INTO Customers(FirstName, LastName, Email, Phone)
VALUES('Caroline', 'Wright', 'CarolineWrightrocketmail.com', '330-333-3448');
SELECT
Customers.LastName,
Customers.FirstName,
Customers.Email
FROM Customers
WHERE Customer_ID= last_insert_id();
大家好。
我试图找出可以同时在触发器内执行两个if语句,以便在输出窗口中一个接一个地显示这两个错误消息。我可以使用for循环或while循环来完成此任务。任何建议都会很棒。这是我上面附带的代码。
答案 0 :(得分:0)
您可以在发信号之前将消息连接在一起:
CREATE TRIGGER Customers_Before_Insert
BEFORE INSERT ON Customers
FOR EACH ROW
BEGIN
DECLARE v_msg varchar(255) DEFAULT '';
IF (NEW.Email NOT LIKE '%_@%_.__%') THEN
set v_msg = 'The email column doesn''t contain @ symbol or .(dot) domain';
END IF;
IF (NEW.Phone NOT REGEXP '[0-9]{3}-[0-9]{3}-[0-9]{4}') THEN
set v_msg = concat_ws('\n', v_msg, 'The Phone Number Needs to be at least 10 digits long');
END IF;
IF v_msg <> '' THEN
SIGNAL SQLSTATE VALUE '45000' SET MESSAGE_TEXT = v_msg;
END IF ;
END; //
您不能在一个触发器中发出两次信号,因为信号结束了处理。因此,构建您想要构建的消息。