mysql syntax error store procedure - mysql

I'm trying to create a procedure but its get an error.
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 4.
Line 4 is "INSERT INTO TB...."
DROP PROCEDURE IF EXISTS simpleproc;
CREATE PROCEDURE simpleproc (IN p_cd_empresa INT, IN p_cd_servico INT, IN p_cd_concessionaria VARCHAR (100))
BEGIN
IF p_cd_servico = 1 OR p_cd_servico = 3 OR p_cd_concessionaria = 'GESTAO' THEN
INSERT INTO TB_EMPRESA_IMPORTAR VALUES (p_cd_empresa, p_cd_servico, p_cd_concessionaria);
END IF;
END;

You forgot to put another delimiter than ; for multi-statement procedures / functions.
DELIMITER $$
DROP PROCEDURE IF EXISTS simpleproc;
CREATE PROCEDURE simpleproc (IN p_cd_empresa INT, IN p_cd_servico INT, IN p_cd_concessionaria VARCHAR (100))
BEGIN
IF p_cd_servico = 1 OR p_cd_servico = 3 OR p_cd_concessionaria = 'GESTAO' THEN
INSERT INTO TB_EMPRESA_IMPORTAR VALUES (p_cd_empresa, p_cd_servico, p_cd_concessionaria);
END IF;
END;
DELIMITER ;

Related

Issue with procedure in MySQL

I have the following procedure (one of my first procedures in MySQL):
DROP PROCEDURE IF EXISTS usp_deposit_money;
DELIMITER $$
CREATE PROCEDURE usp_deposit_money(IN account_id INT,IN money_amount
DECIMAL(19,4))
BEGIN
START TRANSACTION;
UPDATE accounts
SET accounts.balance = accounts.balance + money_amount
WHERE accounts.id = account_id;
IF money_amount <= 0
THEN
ROLLBACK;
ELSE
COMMIT;
END IF;
END $$
DELIMITER ;
But I get the following error message:
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 'DELIMITER $$
CREATE PROCEDURE usp_deposit_money(IN account_id INT,IN money_amou' at line 1

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 Version '5.7.14' SET pProd := pFac1 * pFac2;

The following 'CREATE PROCEDURE' statement from MySQL Connector/Python Developer Guide is not working in MySQL Version '5.7.14':
CREATE PROCEDURE multiply(IN pFac1 INT, IN pFac2 INT, OUT pProd INT)
BEGIN
SET pProd := pFac1 * pFac2;
END;
Please help fix this error:
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 '' at line 3
You probably need a delimiter wrapper
DROP PROCEDURE IF EXISTS multiply;
DELIMITER $$
CREATE PROCEDURE multiply(IN pFac1 INT, IN pFac2 INT, OUT pProd INT)
BEGIN
SET pProd := pFac1 * pFac2;
END;$$
DELIMITER ;

Stored Procedure Syintax

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;
....

how to works on insert,update data from a table using store Procedure

if i use one table in two different Stored procedure name, (one for insert , one for update command) it showing syntax error.
first i created studentrc SP:
delimiter //
create procedure studentrc(in student_name varchar(20),in Reg_no int(6),in mark1 int(3), in mark2 int(3),in total int(10))
begin
insert into studentrecords values(student_name,Reg_no,mark1,mark2,total);
end; //
no errors
next i create studentrcs SP:
delimiter //
create procedure studentrcs(inout Reg_no int(6))
begin
UPDATE studentrecords
set student_name=?,mark1=?,mark2=?,total=?
where Reg_no=?;
end;//
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 'UPDATE studentrecords
set student_name=?,mark1=?,mark2=?,total=?
where Reg_no=' at line 3
how can rectify this error...
I'll be the first to admit I'm not a SQL guru by any means, but shouldn't you be taking in more variables and using them in place of the question marks?
You need to put the news values in the args list
create procedure studentrcs(sname varchar(100),m1 varchar(100),m2 varchar(100),total int,inout Reg_noarg int(6))
begin
UPDATE studentrecords
set student_name=sname ,mark1=m1,mark2=m2,total=totalarg
where Reg_no=Reg_noarg ;
for "INSERT"
DELIMITER $$
DROP PROCEDURE IF EXISTS `mydatabase`.`myprocedurename` $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `myprocedurename`(IN field1 VARCHAR(50),
IN field2 INT(10),IN field3 INT(10), IN field4 VARCHAR(10))
BEGIN
INSERT INTO mytablename (FIELD_1,FIELD_2,FIELD_3,FIELD_4) VALUES
(field1,field2,field3,field4);
END $$
DELIMITER ;
size which i have given in IN parameter should be equal to field size in table
for "UPDATE"
DELIMITER $$
DROP PROCEDURE IF EXISTS `mydatabase`.`myprocedurename` $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `myprocedurename`(IN field1 VARCHAR(50),
IN field2 INT(10) )
BEGIN
UPDATE mytablename SET FIELD_1=filed1 WHERE FIELD_2=field2;
END $$
size which i have given in IN parameter should be equal to field size in table
hope this may help you.