MySQL stored procedure # syntax error - mysql

I want to be able to pass arguments to stored procedure, so I searched the net and encountered something like this:
DELIMITER $$
CREATE PROCEDURE addTmpUser
#id varchar(10)
AS
BEGIN
//some sql code
END$$
DELIMITER ;
The problem is that I am getting a syntax error for the # character.
Note: I am using MySQL db.

You are mixing variable types.
#variable is a user variable with a scope for the entire connection.
The variables in stored procedures look different, they don't have the # before them.
Also, you need to declare them. Here is an example
DELIMITER $$
CREATE PROCEDURE addTmpUser(p_id varchar(10))
-- the variable is named p_id as a nameing convention.
-- It is easy for variables to be mixed up with column names otherwise.
BEGIN
DECLARE innerVariable int;
insert into user (id) values (p_id);
-- return all users
select * from user;
END$$
DELIMITER ;
-- and now call it
call addTmpUser(10);

You need to use IN,OUT,INOUT to specify the parameter. So you can try this
DELIMITER $$
CREATE PROCEDURE addTmpUser (IN id VARCHAR(10))
BEGIN
//some sql code
END$$
DELIMITER ;
Look at the documentation

Related

Mysql simple stored procedure syntax error

I write a simple mysql stored procedure in workbench, however, it complains syntax error. What is wrong with my syntax?
CREATE DEFINER=`root`#`localhost` PROCEDURE `SelectIndicatorsByExistReferenceID`()
BEGIN
select * from indicators;
END
Check out this link for a simple example of a Stored Procedure.
To your question: When you define a SP, you always start with setting a new delimiter in order to use the normal delimiter (;) in your SP. If you don't do this, SQL thinks that you finished your query after the smicolon, which isn't at the end of your query so it throws an error. To set a new delimiter, do the following:
DELIMITER //
CREATE DEFINER=`root`#`localhost` PROCEDURE `SelectIndicatorsByExistReferenceID`()
BEGIN
SELECT * FROM indicators;
...
END //
DELIMITER ;
So now, you first set the delimiter to // and sql just stores the simicolons in your SP. At the END, you say that your query is done and type // - the new delimiter. Then you set it back to the normal simicolon in order to continue as always.
You'd better post your error messages, even though I can guess the problem you are suffering.
The default delimiter of MySQL is ";". So MySQL Workbench treat the statement below as a complete statement:
CREATE DEFINER=`root`#`localhost` PROCEDURE `SelectIndicatorsByExistReferenceID`()
BEGIN
select * from indicators;
It's definitely wrong!
So you should change the delimiter expilictly when you write procedures. Here is an example:
delimiter // -- change the delimiter temporarily
CREATE DEFINER=`root`#`localhost` PROCEDURE `SelectIndicatorsByExistReferenceID`()
BEGIN
select * from indicators;
END //
delimiter ; -- restore the default delimiter
DELIMITER $$
CREATE DEFINER=`root`#`locahost` PROCEDURE `SelectIndicatorsByExistReferenceID`()
BEGIN
SELECT * from indicators;
END $$
DELIMITER ;
Use this
DELIMITER $$
CREATE DEFINER=`root`#`locahost` PROCEDURE `SelectIndicatorsByExistReferenceID`()
BEGIN
SELECT * from indicators;
END $$
DELIMITER ;

empty MySQL stored procedure not compiling

Below MySQL procedure code flags error at line 1. If I remove the arguments of below stored procedure it compiles fine.
DELIMITER $$
CREATE PROCEDURE test.USER_INPUT(#TITLE VARCHAR(20), #SEVERITY INT, #CLOSEDATE DATETIME)
BEGIN
END
DELIMITER ;
what's wrong in the above code? where is the problem exactly?
There are two problems:
You are missing a delimiter after END
Your parameter names contain in invalid character #. Either quote the column names, if you want to keep the #, or remove it.
What will work is:
DELIMITER $$
CREATE PROCEDURE test.USER_INPUT(TITLE VARCHAR(20), SEVERITY INT, CLOSEDATE DATETIME)
BEGIN
END$$
DELIMITER ;
Also make sure the database test exists.

how to cast in MySQL stored procedures

Trying to create the following routine in MySQL Workbench yields a "This object's DDL statement contains syntax errors. Are you sure you want to apply the DDL statement unchanged?":
-- --------------------------------------------------------------------------------
-- Routine DDL
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------------------------------------------
DELIMITER $$
CREATE PROCEDURE `dbName`.`testFunc` ()
BEGIN
SET i = CAST(100 AS DOUBLE);
END
Any ideas?
This does the same thing:
-- --------------------------------------------------------------------------------
-- Routine DDL
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------------------------------------------
DELIMITER $$
CREATE PROCEDURE `rateGenius`.`testFunc` ()
BEGIN
SET i = CONVERT(100, DOUBLE);
END
You need to declare i in your stored procedure.
DELIMITER $$
CREATE PROCEDURE `rateGenius`.`testFunc` ()
BEGIN
DECLARE i DOUBLE
SET i = 100.0;
END
Not sure what you're actually trying to do with this stored proc though. Your CAST/CONVERT is unecessary in this case but your syntax for those functions is in fact correct.

Getting errors when trying to create a PROCEDURE in mysql

I am trying to create a mysql stored procedure, but I get this error:
Script line: 2 Failed to CREATE PROCEDURE proc_test_bideep
The stored procedure code is:
DELIMITER $$
DROP PROCEDURE IF EXISTS `commun`.`insert_categorie` $$
CREATE PROCEDURE `commun`.`insert_categorie` (id_mere INT,
lib_categ VARCHAR(50),
id_categ_sup INT ,
categ_authInstantBuy INT)
BEGIN
SET #bg_mere := (SELECT categ_bg FROM categ_basic WHERE categ_id = id_mere);
#bg_mere+2,categ_level_bideep,categ_statut,categ_adult,categ_authSmallBid,categ_authBid,categ_authInstantBuy);
SELECT '1' AS code_retour; END IF;
ecetera.........
END $$
DELIMITER ;
a) You need to DECLARE any variables on the first lines of the procedure, including their datatype:
DECLARE bg_mere INT;
b) To fetch a value from the database into a variable, you use SELECT ... INTO syntax:
SELECT categ_bg INTO bg_mere FROM categ_basic WHERE categ_basic.categ_id = id_mere;
c) You have an END IF without the corresponding IF.
d) The closing END needs a semicolon (not BEGIN though), only then do you need a delimiter to finish the entire statement, and finally you should reset the delimiter back to normal:
BEGIN
# body of the stored procedure goes here
END;
$$
DELIMITER ;
Your parameters are missing the keyword IN such as: ...(IN id_mere INT, IN lib_categ ...). Also, you need to configure your OUT variable for #bg_mere in the initial parameter list such as (IN xxx, ..., OUT bg_mere VARCHAR/INT/WHATEVER).

Triggers and stored proc in MySQL

I used MSSQL stored procedures and triggers for a while; MySQL is getting me absolutely crazy about how to write even the simpler procedure.
Why I get a syntax error in this so stuoid trigger?
CREATE TRIGGER set_prio_default BEFORE INSERT ON categories
FOR EACH ROW
BEGIN
set #my_prio := 1;
SET new.prio := #my_prio;
END
In facts this TRIGGER is an oversemplification of:
DELIMITER $$
DROP PROCEDURE IF EXISTS `slot08`.`test` $$
CREATE PROCEDURE `slot08`.`test` ()
BEGIN
select 1 + max(prio) from categories INTO #my_prio;
select #my_prio;
END $$
DELIMITER ;
Still i do not understand how to use variables in procedures. If I use a DECLARE statement and the variable name miss the # character I got an error from mysql telling me "unknown system variable" - but many examples I saw used this syntax
I mean:
this does not work
CREATE TRIGGER set_prio_default BEFORE INSERT ON categories
FOR EACH ROW
BEGIN
declare my_prio integer default 1;
set my_prio := 1;
SET new.prio := my_prio;
END
If I use # I get syntax error.
Have some hints?
Thanks!
I dont think you have to use the := operator. A simple equals will do. All variables declared inside the stored procedure must be prefixed with the #symbol.
Hey, found the answer. Hope that people with so little experience as me in MySQL procedures could avoid to spend the time I have spent on the same issue. This does work:
DELIMITER $$
CREATE TRIGGER blablabla
BEFORE INSERT ON myStupidTable
FOR EACH ROW
BEGIN
declare my_prio integer;
select 1 + max(prio) from myStupidTable INTO my_prio;
SET new.prio := my_prio;
END $$
DELIMITER ;
It seems that the MySQL syntax errors experienced so far were a delimiter issue.
Greetings
Daniel