mySQL stored procedure error - mysql

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 $$

Related

How to create a stored proceedure in mysql?

Just trying to make a procedure that copies one table into a new one but keep getting all sorts of errors..
eg:
SQL Error [1064] [42000]: 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 spClean_T()
BEGIN
CREATE TABLE staging_table AS ' at line 2
DELIMITER $$
CREATE PROCEDURE spClean_T()
BEGIN
CREATE TABLE staging_table AS SELECT * FROM raw_api_data
END$$
DELIMITER ;`
thanks
You have to end all commands with a semicolon or else mysql will search for more code
DELIMITER $$
CREATE PROCEDURE spClean_T()
BEGIN
CREATE TABLE staging_table AS SELECT * FROM raw_api_data;
END$$
DELIMITER ;
But if you don't have more commands
Make a simple without DELIMITER
CREATE PROCEDURE spClean_T()
CREATE TABLE staging_table AS SELECT * FROM raw_api_data;

How do I create a stored procedure in heidisql?

I'm trying to create a stored procedure in heidisql (mysql).
CREATE PROCEDURE SP_FORM20_POST(
P_SESSIONID VARCHAR(256)
)
BEGIN
INSERT INTO tbForm20
( SESSIONID, RegDT)
VALUES
( P_SESSIONID, NOW());
END
This is my query. I'm trying to create this procedure, but occur some error:
Error code is 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'' at line 8".
However, I don't know wrong syntax. What is wrong?
I want to success in heidisql tool. I don't want other db tool.
Please help me.
The problem in that query is the semicolon, which is the default query delimiter.
In order to change the delimiter in HeidiSQL, add the DELIMITER client command above the CREATE PROCEDURE query:
DELIMITER \\
SELECT 1\\
CREATE PROCEDURE ...\\
DELIMITER ;
HeidiSQL also has a procedure designer, with no need to set the delimiter:
The problem is this. The database reads the ; in your code as an end of the procedure. You probably don't intend it like that :). The DELIMITER command takes care of that by changing ; to something customizable, like $$. Try this:
DELIMITER $$
CREATE PROCEDURE SP_FORM20_POST(
P_SESSIONID VARCHAR(256)
)
BEGIN
INSERT INTO tbForm20
( SESSIONID, RegDT)
VALUES
( P_SESSIONID, NOW());
END$$
DELIMITER ;
Try this one
DELIMITER //
CREATE PROCEDURE SP_FORM20_POST(
P_SESSIONID VARCHAR(256)
)
BEGIN
INSERT INTO tbForm20
( SESSIONID, RegDT)
VALUES
( P_SESSIONID, NOW());
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;
....

error in writing procedure in Phpmyadmin

I'm trying to write a stored procedure in Mysql phpmyadmin, the procedure is
DELIMITER $$
DROP FUNCTION IF EXISTS `shopping_portal`.`f_authenticate_admin`$$
CREATE PROCEDURE =`root`#`localhost` FUNCTION `f_authenticate_admin`(l_username VARCHAR(50),l_password VARCHAR(50)) RETURNS int(11)
BEGIN
DECLARE exist INT DEFAULT 0;
SELECT count(*) INTO exist FROM admin WHERE username=l_username and password=MD5(l_password);
RETURN exist;
END$$
DELIMITER ;
but it is throwing the 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 '=`root`#`localhost` FUNCTION `f_authenticate_admin`(l_username VARCHAR(50),l_pas' at line 1
Help me in writing this procedure. Thanks in advance.
Well, multiple issues:
You're mixing procedures and functions, those are two different stories. And you're probably looking for DEFINER with your = root#localhost. And you want to use single quotes instead of backticks (I'm not sure though, if that's really an issue). Anyway, let me rewrite it for you...
DROP PROCEDURE IF EXISTS `shopping_portal`.`f_authenticate_admin`;
DELIMITER $$
CREATE DEFINER = 'root'#'localhost' PROCEDURE `f_authenticate_admin`(IN l_username VARCHAR(50), IN l_password VARCHAR(50), OUT result tinyint)
BEGIN
SELECT EXISTS(SELECT 1 FROM admin WHERE username=l_username and password=MD5(l_password)) INTO result;
END$$
DELIMITER ;
You then call it like this:
CALL f_authenticate_admin('test_username', 'a_password', #a_variable);
Then you have your result in #a_variable.
SELECT #a_variable;
Result is either 1 or 0.

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.