第一次写mysql存储过程报错保存不了,求解
CREATEDEFINER=CURRENT_USERPROCEDURE`NewProc`(IN`spid`int,IN`spprice`varchar,IN`spnum`...
CREATE DEFINER = CURRENT_USER PROCEDURE `NewProc`(IN `spid` int,IN `spprice` varchar,IN `spnum` varchar)BEGIN DECLARE wareid INT; //定义三个变量 DECLARE num1 INT; DECLARE num2 INT; DECLARE cursor_invent CURSOR FOR SELECT goodsid FROM goods; /*定义游标 返回参数对应的商品 id*/ OPEN cursor_invent; REPEAT FETCH cursor_invent INTO wareid; //将id赋值给wareid IF wareid=spid THEN SET num1=CONVERT(inventnum,SIGNED); //varchar转化成int SET num2=CONVERT(spnum,SIGNED); UPDATE invent SET inventnum=CONCAT(num1+num2) WHERE inventid=spid; //如果存在同名商品那么原有数量+现有数量 ELSE INSERT INTO invent(goodsid,inventnum,inventprice) values(spid,spprice,spnum); //不存在就直接插入一条记录 END IF; UNTIL cursor_invent END REPEAT; CLOSE cursor_invent;END;
展开
1个回答
展开全部
有两个问题
1、参数中varchar要定义几个字符,如varchar(20)
2、mysql的注释方式没有//,只有/**/和#
3、存储过程的前后要先定义分隔符DELIMITER ;;
因此最终结果:
DELIMITER ;;
CREATE DEFINER=`root`@`%` PROCEDURE `NewProc`(IN `spid` int,IN `spprice` varchar(20),IN `spnum` varchar(20))
BEGIN
DECLARE wareid INT; #定义三个变量
DECLARE num1 INT;
DECLARE num2 INT;
DECLARE cursor_invent CURSOR FOR SELECT goodsid FROM goods; /*定义游标 返回参数对应的商品 id*/
OPEN cursor_invent;
REPEAT
FETCH cursor_invent INTO wareid; #将id赋值给wareid
IF wareid=spid
THEN
SET num1=CONVERT(inventnum,SIGNED); #varchar转化成int
SET num2=CONVERT(spnum,SIGNED);
UPDATE invent SET inventnum=CONCAT(num1+num2) WHERE inventid=spid; #如果存在同名商品那么原有数量+现有数量
ELSE INSERT INTO invent(goodsid,inventnum,inventprice) values(spid,spprice,spnum); #不存在就直接插入一条记录
END IF;
UNTIL cursor_invent END REPEAT;
CLOSE cursor_invent;
END
;;
DELIMITER ;
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询