创建存储过程时出错

时间:2018-02-21 22:27:36

标签: mysql stored-procedures

我正在尝试通过连接两个表(客户和查询表)来检索值,然后我试图将检索到的值存储到另一个表中,这个表会因为与此无关的原因而派上用场。然后我就是最后从查询表中删除检索到的值。当我尝试执行存储过程时,我收到以下屏幕截图中显示的以下错误。 MySQL Error

如何解决此错误?

存储过程: -

CREATE PROCEDURE `backup_eq`(
    IN `eq` VARCHAR(15), IN `mail` VARCHAR(30), IN `dates` DATE, IN `cmp` VARCHAR(10), IN `rea` VARCHAR(50)) 
NOT DETERMINISTIC MODIFIES SQL DATA SQL SECURITY DEFINER 
BEGIN 
SELECT eqno into @eno,Date1 into @d,cmpname into @c,subject into @s,cid into @cd 
FROM `enquiry` NATURAL JOIN `customer` 
WHERE eqno=eq and email=mail and cmpname=cmp and Date=dates; 
INSERT INTO `enquiryBin`(`Eqno`, `Date1`, `Cmpname`, `Subject`, `CID`, `Reason`) 
VALUES (@eno,@d,@c,@s,@cd,rea); 
DELETE FROM `enquiry` 
WHERE eqno=eq and cid=@cd and cmpname=cmp and Date1=dates; 
END

这两个表的create table语句如下所示

CREATE TABLE `customer` (
  `CID` int(15) NOT NULL,
  `Address` varchar(100) NOT NULL,
  `Name` varchar(20) NOT NULL,
  `email` varchar(30) NOT NULL,
  `phone` bigint(20) NOT NULL
)
ALTER TABLE `customer`
  ADD PRIMARY KEY (`CID`);

  CREATE TABLE `enquiry` (
  `Eqno` varchar(15) NOT NULL,
  `Date1` date NOT NULL,
  `Cmpname` varchar(10) NOT NULL,
  `Subject` varchar(100) NOT NULL,
  `CID` int(15) NOT NULL
)

  ALTER TABLE `enquiry`
  ADD PRIMARY KEY (`Eqno`,`Cmpname`,`CID`,`Date1`)

2 个答案:

答案 0 :(得分:2)

SELECT eqno into @eno,Date1 into @d,cmpname into @c,subject into @s,cid into @cd

应该是

SELECT eqno, Date1, cmpname, subject, cid INTO @eno, @d, @c, @s, @cd

也就是说,将select-list中的所有列与INTO子句分开命名。

请参阅语法文档:https://dev.mysql.com/doc/refman/5.7/en/select-into.html

答案 1 :(得分:1)

不需要所有这些变量,只需在INSERT INTO ... SELECT查询中使用JOIN查询和DELETE

INSERT INTO enquiryBin (`Eqno`, `Date1`, `Cmpname`, `Subject`, `CID`, `Reason`)
SELECT eqno, Date1, cmpname, subject, cid, rea
FROM FROM `enquiry` NATURAL JOIN `customer` 
WHERE eqno=eq and email=mail and cmpname=cmp and Date1 = dates; 

DELETE e FROM enquiry AS e
NATURAL JOIN customer 
WHERE eqno = eq AND email = mail AND cmpname = cmp AND Date1 = dates