在MySQLWorkbench中声明变量语法错误?

Declare variable syntax error in MySQL Workbench?

在MySQLWorkbench中声明变量语法错误?代码没问题,但错误是 DECLARE cod int;

CREATE PROCEDURE sp_insertarPro(
   nom  varchar(100),
   pre  decimal(18, 2),
   img  varchar(100)
)

DECLARE cod int;
SELECT IFNULL(MAX(codigoProducto),0)+1 into cod FROM producto;
INSERT INTO producto VALUES (cod,nom,pre,img);

错误:

create procedure sp_insertarPro(nom  varchar(100),pre decimal(18, 2),img  varchar(100))  declare cod int    

Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'declare cod int' at line 7 

试试这个:

      DELIMITER $$
      CREATE PROCEDURE sp_insertarPro(
        nom  varchar(100),
        pre  decimal(18, 2),
        img  varchar(100)
      )
      BEGIN
      DECLARE cod int;
      SELECT IFNULL(MAX(codigoProducto),0)+1 into cod FROM producto;
      INSERT INTO producto VALUES (cod,nom,pre,img);
      END $$
      DELIMITER ;