Stored Procedure Syintax - mysql

I want to do write a stored procedure using MysQL, that let me insert, update or delete using parameters, into persona's table, who has the fields id_persona, nombres, apellidos & edad; so can you give me an idea why this is wrong about my stored procedure?
DELIMITER $$
DROP PROCEDURE IF EXISTS guardarPersona$$
CREATE PROCEDURE persona(input_id_persona INT, input_nombres VARCHAR(100), input_apellidos VARCHAR(100), input_edad INT, input_accion VARCHAR(15))
BEGIN
IF (input_accion='guardar') THEN
INSERT INTO `persona`(`nombres`, `apellidos`, `edad`) VALUES (input_nombres,input_apellidos,input_edad);
ELSEIF (input_accion='modificar') THEN
UPDATE `persona` SET `nombres`=input_nombres,`apellidos`=input_apellidos,`edad`=input_edad WHERE id_persona = input_id_persona;
ELSEIF (input_accion='eliminar') THEN
DELETE FROM `persona` WHERE id_persona = input_id_persona;
ELSE
mysql_error();
END IF;
END $$
DELIMITER ;
This is the mysql error....
MySQL ha dicho: Documentación
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '();
END IF;
END' at line 10

mysql_error() function is using in API.
For get data of error inside stored procedure you should use SHOW ERRORS;
...
ELSE
SHOW ERRORS;
END IF;
....

Related

Mysql Procedure Syntax error on Update

DELIMITER //
DROP PROCEDURE IF EXISTS pad_fato_to_tad_fato//
CREATE PROCEDURE pad_fato_to_tad_fato(IN IDFATO BIGINT, IN UCI BIGINT)
BEGIN
INSERT INTO tad_fato (FAT_UCI, FAT_DESCRICAO,
FAT_DATA_CIENCIA_AUTORIDADE,
FAT_CADASTRANTE, FAT_DATA_CAD)
SELECT FAT_UCI, FAT_DESCRICAO, FAT_DATA_CIENCIA_AUTORIDADE,
FAT_CADASTRANTE, FAT_DATA_CAD
FROM pad_fato WHERE (FAT_ID = IDFATO);
END//
BEGIN
UPDATE tad_termo_de_ajustamento SET TAD_STATUS_ID="2" WHERE (TAD_FK_PRE_UCI = UCI);
END//
DELIMITER ;
Error (12,1): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near UPDATE tad_termo_de_ajustamento SET TAD_STATUS_ID="2" WHERE (TAD_FK_PRE_UCI=UCI) at line 2
You have double "END" word after each query.
And not pretty formatted code :)
DELIMITER //
DROP PROCEDURE IF EXISTS pad_fato_to_tad_fato//
CREATE PROCEDURE pad_fato_to_tad_fato(IN IDFATO BIGINT, IN UCI BIGINT)
BEGIN
INSERT INTO tad_fato (
FAT_UCI,FAT_DESCRICAO,
FAT_DATA_CIENCIA_AUTORIDADE,
FAT_CADASTRANTE,FAT_DATA_CAD
)
SELECT
FAT_UCI,
FAT_DESCRICAO,
FAT_DATA_CIENCIA_AUTORIDADE,
FAT_CADASTRANTE,
FAT_DATA_CAD
FROM pad_fato
WHERE (FAT_ID=IDFATO);
UPDATE tad_termo_de_ajustamento
SET
TAD_STATUS_ID="2"
WHERE (TAD_FK_PRE_UCI=UCI);
END//
DELIMITER ;

MySQL syntax for UPDATE with an IF conditional in a stored procedure?

I am trying run a stored procedure but its getting failed due to IF condition.I have tried with different ways still no success. can anyone let me know, how to use IF condition in the stored procedure.
DELIMITER $$
USE `testdb`$$
DROP PROCEDURE IF EXISTS `change_parent`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `change_parent`(
IN new_parent BIGINT(20),
IN folder__id BIGINT(20)
)
BEGIN
SELECT
directory_path,nav_depth,id
INTO
#dirpath,#depth,#parent
FROM folders
WHERE id = new_parent;
UPDATE folders
IF (folder__id != #parent) THEN
SET directory_path = CONCAT(#dirpath,'/',title),
nav_depth = #depth+1,
parent__id = #parent
END IF;
WHERE id = folder__id;
END$$
DELIMITER ;
Here is the error message
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: 'IF folder__id != #parent THEN SET directory_path = CONCAT(#dirpath,'/',title' at line 13
IF (folder__id != #parent) THEN
UPDATE folders
SET ...
WHERE id = folder__id;
END IF;

Error 1064 mysql syntax

I am new to Mysql and I am trying to write mysql stored procedure with transaction.but I am getting error:
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 '' at line 21
Here is my procedure:
DELIMITER $$
CREATE PROCEDURE InsertVote(IN image_param VARCHAR(30),
IN user_id_param INT,
IN vore_char CHAR(1)
)
Begin
START TRANSACTION;
IF vote_char="A" THEN
UPDATE img_options SET A_result=(A_result+1) WHERE image=image_param;
ELSE IF vote_char="B" THEN
UPDATE img_options SET B_result=(B_result+1) WHERE image=image_param;
ELSE IF vote_char="C" THEN
UPDATE img_options SET C_result=(C_result+1) WHERE image=image_param;
ELSE
UPDATE img_options SET D_result=(D_result+1) WHERE image=image_param;
END IF;
Insert into WATCH_FEED_EVEN values(image_param,user_id_param);
select * from img_options where image=image_param;
COMMIT; <---Line No 21
END $$
DELIMITER ;
I tried to solve this one for more than 2 hours but I couldn't able to do this.Can anyone help me?

mySQL stored procedure error

I am trying to use a if statement in my stored mySQL procedure, but when I try to create it in mySQL workbench I get this error ERROR 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 'database'.'table' WHERE date=dateIn;.
Here is the code:
DELIMITER $$
CREATE DEFINER=`rates_d_db` PROCEDURE `byDate`(in dateIn VARCHAR(255),in action VARCHAR(255))
BEGIN
IF action = "edit" THEN EDIT `database`.`table` WHERE date=dateIn;
ELSE SELECT * FROM `database`.`table` WHERE date=dateIn;
END IF;
END$$
I am new to stored procedures, so it's probably a very noob mistake.
Thanx in advance!
date is a reserved word in mySQL. You will have to wrap that in backticks as well.
Here is correct version of your procedure
DELIMITER $$
CREATE DEFINER=`rates_d_db` PROCEDURE `byDatee`(in dateIn VARCHAR(255),in action VARCHAR(255))
BEGIN
IF action = "edit" THEN
-- I used select below as i don't know what you want in edit either alter table or update table
SELECT * FROM `database`.`table` WHERE date=dateIn;
ELSE
SELECT * FROM `database`.`table` WHERE date=dateIn;
END IF;
END $$

Mysql stored procedure error

I'm trying to add the following stored procedure to my mysql database:
CREATE PROCEDURE logmsg ( _Username VARCHAR(50), _Message VARCHAR(80) )
BEGIN
INSERT INTO chatlogs (sender, message) VALUES (_Username, _Message);
END;
But its failing the query and returning:
#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 '' at line 3
I've been searching for about 2 hours on google and cannot find any answer at all.
Any help is greatly appreciated!
While I'm not 100% sure because I can't test on a MySQL server at the moment, I think the problem is in the semicolon. On the line with INSERT you basically end the CREATE PROCEDURE statement, which has incorrect syntax this way. You have to set the delimiter to something else (e.g. //), to be able to use the semicolon in the body of the procedure:
delimiter //
CREATE PROCEDURE logmsg ( _Username VARCHAR(50), _Message VARCHAR(80) )
BEGIN
INSERT INTO chatlogs (sender, message) VALUES (_Username, _Message);
END//
delimiter ;