Mysql stored procedure error - mysql

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 ;

Related

MySQL query unknown error

I'm trying to run this query on MySQL server:
CREATE PROCEDURE forum.eventlog_create(
i_UserID INT,
i_Source VARCHAR(128),
i_Description TEXT,
i_Type INT,
i_UTCTIMESTAMP DATETIME)
MODIFIES SQL DATA
BEGIN
INSERT INTO forum.EventLog
(UserID, Source, Description, ´Type´)
VALUES (i_UserID, i_Source, i_Description, i_Type);
END;
However upon executing it I get the following 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 12
and I'm unable to fix it. I tried to search for a solution and asked a co-worker but we were unable to find the solution, as last resort I decided to ask it here.
I get error code 1064 but the right syntax near '' is the message and I dont understand what the problem could be. It would be easier if it said which syntax gives the error, I only get the line number.
Thank you for your time
There is one error caused by the escape character around type which should be either backticks or dropped and you should try setting delimiters https://dev.mysql.com/doc/refman/5.7/en/stored-programs-defining.html
delimiter $$
CREATE PROCEDURE eventlog_create(
i_UserID INT,
i_Source VARCHAR(128),
i_Description TEXT,
i_Type INT,
i_UTCTIMESTAMP DATETIME)
MODIFIES SQL DATA
BEGIN
INSERT INTO forum.EventLog
(UserID, Source, Description, `Type`)
VALUES (i_UserID, i_Source, i_Description, i_Type);
END $$
delimiter ;
Try this
DELIMITER $$
CREATE PROCEDURE forumeventlog_create()
BEGIN
declare i_UserID INT DEFAULT 0;
declare i_Source VARCHAR(128) DEFAULT null;
declare i_Description TEXT DEFAULT null;
declare i_Type INT DEFAULT 0;
declare i_UTCTIMESTAMP DATETIME DEFAULT null ;
INSERT INTO forum.EventLog
(UserID, Source, Description, ´Type´)
VALUES (i_UserID, i_Source, i_Description, i_Type);
END $$
DELIMITER ;
You can call this by
CALL forumeventlog_create()
OR
DELIMITER $$
CREATE PROCEDURE forumeventlog_create(i_UserID INT,i_Source VARCHAR(128),i_Description TEXT,i_Type INT,i_UTCTIMESTAMP DATETIME)
BEGIN
INSERT INTO forum.EventLog
(UserID, Source, Description, ´Type´)
VALUES (i_UserID, i_Source, i_Description, i_Type);
END $$
DELIMITER ;

MySQL 6.3 - Create Procedure Error in Syntax

I am trying to create a stored procedure but getting error. Stored Procedure is as given below
CREATE PROCEDURE addSection (IN sectionname varchar(50), IN sectiondetail varchar(50))
BEGIN
INSERT INTO inquiry (sectionname,sectiondetail,entrytime) VALUES (sectionname,sectiondetail,now());
END// delimiter;
But I am getting error as given below
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 0.00026 sec
I am using MySQL Workbench 6.3
Please help...
Run the following first and switch the delimiter to //:
delimiter //
Then run what you had with a minor modification (compare the last line):
CREATE PROCEDURE addSection (IN sectionname varchar(50), IN sectiondetail varchar(50))
BEGIN
INSERT INTO inquiry (sectionname,sectiondetail,entrytime) VALUES (sectionname,sectiondetail,now());
END//
Without switching delimiter, when the client sees the semicolon at the end of the insert statement, it thought the definition of the procedure ended there prematurely - that's what the syntax error was about.
You can always switch the delimiter back to ; by doing:
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;
....

error while creating mysql procedure having SYS_REFCURSOR as out param

I'm creating procedure which is having two parameters , one is p_cursor of type SYS_REFCURSOR (OUT param) and the other one is p_rank of type INT(IN param). But it showing an error.
DELIMITER $$
CREATE PROCEDURE sp_student(p_cursor OUT SYS_REFCURSOR,p_rank IN INT)
BEGIN
OPEN p_cursor FOR SELECT * FROM student WHERE rank = p_rank;
END$$
DELIMITER ;
the error what I'm getting is,
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 'OUT SYS_REFCURSOR,p_rank IN INT)
BEGIN
OPEN p_cursor FOR SELECT * FROM st' at line 1
I think I'm syntactically wrong for SYS_REFCURSOR.. please check my code and let me realise my mistake.
thanks in advance
mysql doesnt have refcursor like oracle, if u r planning to write a stored procedure that returns multiple rows/result set in mysql just do
DROP procedure IF EXISTS `sample`;
DELIMITER $$
CREATE PROCEDURE `sample`(p_rank IN INT)
BEGIN
select * from MyTable where id=p_rank;
END$$
DELIMITER ;
call sample();
this will return a result set. which u can use.

MySQL Create Procedure Yields Error 1064

Good Morning,
I'm creating the following procedure:
CREATE DEFINER=`root`#`localhost` PROCEDURE `insert_inventory_audit`(
IN `pitem_id` int,
IN `puser_id` int,
IN `pfield_name` varchar(265),
IN `pold_value` mediumtext,
IN `pnew_value` mediumtext
)
BEGIN
INSERT INTO inventory_audit (item_id, user_id, field_name, old_value, new_value)
VALUES (pitem_id, puser_id, pfield_name, pold_value, pnew_value);
END$$
It is being copied to our new server running MySQL 5.5.19 from our old server running MySQL 5.0.45.
When I excecute the above code on the new server, I recieve the following 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 11
Does this mean that each entry inside the VALUES parentheses must be surrounded by '' eg. 'pitem_id' ?
You need to have DELIMITER $$ before the create statement.
You didn't change the delimiter from the default ;, so the ; you're using there is actually terminating the procedure, not the query.
DELIMITER $$ <--- add this line
CREATE ....
...
END$$