Home > Net >  Declare variable syntax error in MySQL Workbench?
Declare variable syntax error in MySQL Workbench?

Time:12-04

Declare variable syntax error in MySQL Workbench? the code is ok but the error is on 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);

ERROR:

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 

CodePudding user response:

Try this:

      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 ;
  • Related