My problem is this, I am trying to create a procedure that receives input parameters and check if the data exists or not, to be able to insert in one table or another. I have tried in different ways and I can not find the error that appears in the syntax, of the declaration.
Please, I need help.
A thousand thanks in advance.
/ Next I show the code of the procedure /
CREATE PROCEDURE InsertMensaje(IN pNOMBRE varchar(50), IN pTLFN varchar(9), IN pEMAIL varchar(50), IN pASN varchar(15), IN pMSG varchar(500))
BEGIN
/*declaro valiable*/
DECLARE vIDCLI int;
/*ESTAS OPCIONES TAMBIÉN HE PROBADO*/
/*SET vIDCLI = (select id_cliente from tb_cliente where email = pEMAIL);*/
/*select id_cliente into vIDCLI from tb_cliente where email = pEMAIL;*/
/* DECLARE vAUX varchar(50)*/
/* SET vAUX = (select email from tb_cliente where email = pEMAIL);*/
/* select email into vAUX from tb_cliente where email = pEMAIL; */
/* IF (vAUX <=> NULL) THEN */
/*compruebo si existe el parámetro pEMAIL*/
IF EXISTS (select id_cliente from tb_cliente where email = pEMAIL) THEN
/*recupero el ID y lo asigno a la variable*/
select id_cliente into vIDCLI from tb_cliente where email = pEMAIL;
/*inserto en BD tabla mensaje*/
INSERT INTO 'tb_msg'('asn', 'msg', 'id_cli') VALUES ('pASN','pMSG','vIDCLI');
ELSE
/*inserto nuevo cliente*/
INSERT INTO 'tb_cliente'('nombre', 'tlfn', 'email')
VALUES ('pNOMBRE','pTLFN','pEMAIL');
/*recupero new IdCli*/
SET vIDCLI = (select id_cliente from tb_cliente where email = pEMAIL);
/*insert en la tabla mensaje*/
INSERT INTO 'tb_msg'('asn', 'msg', 'id_cli') VALUES ('pASN','pMSG',vIDCLI);
END IF;
END; //
DELIMITER ;
MySQL has said:
1064 - Something is wrong in its syntax near 'IF EXISTS (select id_cliente from tb_cliente where email = pEMAIL) THEN' on line 25