how to cast in MySQL stored procedures - mysql

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.

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 ;

how to insert rows via loop in MySQL shell (no PHP)

I would like to execute a loop in phpmyadmin which inserts rows in a table. So far I have:
DELIMITER $$
DROP PROCEDURE IF EXISTS insert_my_rows()
CREATE PROCEDURE insert_my_rows()
BEGIN
DECLARE i INT DEFAULT 376;
WHILE i<405 DO
INSERT INTO wp_term_relationships(object_id,term_taxonomy_id,term_order) VALUES (i,16,0);
SET i=i+1;
END WHILE;
END $$
DELIMITER ;
CALL insert_my_rows()
With this, I get an 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 'DELIMITER$$
DROP PROCEDURE IF EXISTS insert_my_rows()
CREATE PROCEDURE ins' at line 1
Syntax for the DROP PROCEDURE statement is incorrect!
Change
DROP PROCEDURE IF EXISTS insert_my_rows()
to
DROP PROCEDURE IF EXISTS insert_my_rows;
You need to end the statement with the proper delimiter. Change END to END$$.
You must change the delimiter only while you make blocks of statements, so during the procedure definition. The DROP PROCEDURE and CALL statements needs delimiters too.
DROP PROCEDURE IF EXISTS insert_my_rows;
DELIMITER $$
CREATE PROCEDURE insert_my_rows()
BEGIN
DECLARE i INT DEFAULT 376;
WHILE i<405 DO
INSERT INTO wp_term_relationships(object_id,term_taxonomy_id,term_order) VALUES (i,16,0);
SET i=i+1;
END WHILE;
END $$
DELIMITER ;
CALL insert_my_rows();
DROP PROCEDURE IF EXISTS insert_my_rows;

MySQL stored procedure # syntax error

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

How to Alter a stored procedure in mysql

How to Alter a stored procedure in Mysql.
DROP PROCEDURE IF EXISTS sp_Country_UPDATE;
CREATE PROCEDURE sp_Country_UPDATE
( IN p_CountryId int,
IN p_CountryName nvarchar(25),
IN p_CountryDescription nvarchar(25),
IN p_IsActive bit,
IN p_IsDeleted bit )
UPDATE
Country
SET
CountryName = p_CountryName ,
CountryDescription=p_CountryDescription,
IsActive= p_IsActive,
IsDeleted=p_IsDeleted
WHERE
CountryId = p_CountryId ;
How to alter this Stored Procedure?
If you mean you want to edit the Procedure, then you can't according to the MySQL docs:
This statement can be used to change the characteristics of a stored procedure. More than one change may be specified in an ALTER PROCEDURE statement. However, you cannot change the parameters or body of a stored procedure using this statement; to make such changes, you must drop and re-create the procedure using DROP PROCEDURE and CREATE PROCEDURE.
The Alter syntax lets you change the "characteristics" but not the actual procedure itself
http://dev.mysql.com/doc/refman/5.0/en/alter-procedure.html
Here's an example of creating, Altering (the comment) then dropping and recreating:
DROP PROCEDURE myFunc;
DELIMITER //
CREATE PROCEDURE myFunc ()
COMMENT 'test'
BEGIN
SELECT 5;
END //
DELIMITER ;
ALTER PROCEDURE myFunc
COMMENT 'new comment';
CALL myFunc();
DROP PROCEDURE myFunc;
DELIMITER //
CREATE PROCEDURE myFunc ()
COMMENT 'last time'
BEGIN
SELECT 6;
END //
DELIMITER ;
CALL myFunc();
The above CALL myFunc() statments would return 5 and then 6.
Viewing the stored procedure would show a comment of "test", "new comment" or "last time" depending on when you viewed the Procedure body (I'm not sure how to view the comments via the CLI but I can see them in the functions tab in Navicat)
ALTER PROCEDURE proc_name [characteristic ...]
characteristic:
COMMENT 'string'
| LANGUAGE SQL
| { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }
| SQL SECURITY { DEFINER | INVOKER }
This is how you Create
CREATE PROCEDURE GetAllProducts()
BEGIN
SELECT * FROM products;
END //
This is how you Alter
Alter PROCEDURE GetAllProducts()
BEGIN
SELECT * FROM products;
END //

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