SQL插入,更新存储过程

时间:2017-06-29 08:12:26

标签: mysql stored-procedures

CREATE TABLE `db_customer`.`customer` (
  `customer_id` INT NOT NULL AUTO_INCREMENT,
  `first_name` VARCHAR(45) NOT NULL,
  `lastname` VARCHAR(45) NOT NULL,
  `date_of_birth` DATE NOT NULL,
  `country` VARCHAR(45) NOT NULL,
  `email` VARCHAR(100) NOT NULL,
  `username` VARCHAR(45) NOT NULL,
  `short_biography` TEXT NOT NULL,
  `photo` BLOB NULL,
  PRIMARY KEY (`customer_id`)
);

DELIMITER $$     CREATE PROCEDURE table_in(       first_name VARCHAR(45),       lastname VARCHAR(45),       date_of_birth日期,       country VARCHAR(45),       电子邮件VARCHAR(100),       username VARCHAR(45),       short_biography TEXT     )     开始       INSERT INTO客户(         名字,         姓,         出生日期,         国家,         电子邮件,         用户名,         short_biography       )价值(         顾客ID,         名字,         laast_name,         出生日期,         国家,         电子邮件,         用户名,         short_biography     );     END $$

DELIMITER;

2 个答案:

答案 0 :(得分:1)

您必须更改分隔符。

DELIMITER $$

CREATE PROCEDURE `table_in` 
(customer_id, first_name VARCHAR(45), lastname VARCHAR(45), date_of_birth DATE,
country VARCHAR(45), email VARCHAR(100), username VARCHAR(45), short_biography TEXT)
BEGIN
    INSERT INTO customer (first_name, laast_name, date_of_birth, country, email, username, short_biography) VALUES (customer_id, first_name, laast_name, date_of_birth, country, email, username, short_biography);
END$$

DELIMITER ;

然后它会编译。否则,MySQL将按指令解释您的存储过程。

答案 1 :(得分:1)

1)您的列名称与SP内部使用的列不匹配(first_namelaast_name)。

2)customer_id是自动增量,因此不需要在插入块内使用。

3)根据此answer的建议,使用DELIMITER

DELIMITER $$
CREATE PROCEDURE `table_in` 
(first_name VARCHAR(45), lastname VARCHAR(45), date_of_birth DATE,
country VARCHAR(45), email VARCHAR(100), username VARCHAR(45), short_biography TEXT)
BEGIN
    INSERT INTO customer (`name`, lastname, date_of_birth, country, email, username, short_biography) 
    VALUES (first_name, lastname, date_of_birth, country, email, username, short_biography);
END$$

DELIMITER ;