0

我是程序的新手,我正在尝试在我的客户网站中实施它们。

我想创建一个添加新客户的程序。我想检查以确保电话号码不在数据库中。如果是,我想显示一条错误消息。如果电话号码不在数据库中,我想将其插入到我的客户表中。

我已经实现了以下内容,但出现错误。我不确定我是否在 MySQL 中正确声明了变量,并且我不确定“if 语句”是否以我所做的方式有效。另外,如何将 if 语句查询设置为变量?如果有人可以提供帮助,那将意义重大。谢谢!

DELIMITER //
CREATE PROCEDURE add_Customer
(IN customerNum int, 
IN last varchar, 
IN first varchar)
BEGIN
DECLARE output var DEFAULT '';
IF SELECT CUSTOMER.customerNum FROM CUSTOMER WHERE CUSTOMER.customerNum= customerNum 
THEN
SET output= "Customer already exists!";
select output;
ELSE
INSERT INTO CUSTOMER VALUES (customerNum, last, first);
END IF;
END//
DELIMITER;

已编辑

DELIMITER //
CREATE PROCEDURE add_Customer
(IN customerNum int, 
IN last varchar, 
IN first varchar)
BEGIN
DECLARE output var DEFAULT '';
DECLARE var_num INT;
SELECT CUSTOMER.customerNum INTO var_num FROM CUSTOMER WHERE CUSTOMER.customerNum= customerNum 
if var_num IS NULL THEN
INSERT INTO CUSTOMER VALUES (customerNum, last, first);
ELSE
SET output= "Customer already exists!";
select output;
END IF;
END//
DELIMITER;
4

1 回答 1

0

尝试:

DECLARE var_num INT;
SELECT CUSTOMER.customerNum INTO var_num FROM CUSTOMER WHERE CUSTOMER.customerNum= customerNum
IF var_num IS NULL THEN
    INSERT INTO CUSTOMER VALUES (customerNum, last, first);
ELSE
    SET output= "Customer already exists!";
    select output;
ENDIF;
...
于 2012-12-05T04:06:22.947 回答