how to pass parameter to stored procedure using mysql - mysql

Im trying to add parameter to this stored procedure
DELIMITER $$
DROP PROCEDURE IF EXISTS `mifostenant-default`.`test` $$
CREATE PROCEDURE `mifostenant-default`.`test` ()
BEGIN
select * from employees
END $$
DELIMITER ;

Here is the way to make a procedure and pass a parameter to it:
delimiter $$
DROP PROCEDURE IF EXISTS `mifostenant-default`.`test`;
CREATE PROCEDURE `mifostenant-default`.`test` (IN empName CHAR(20))
BEGIN
SELECT * FROM employees WHERE employee_name=empName;
END $$
delimiter ;
And you call your procedure as below:
CALL mifostenant-default.test(the_parameter_to_pass);

Related

MySQL returns value, but variable is null

I have this stored procedure:
DROP PROCEDURE IF EXISTS rankvolume $$
CREATE PROCEDURE `rankvolume`(IN theranking INT, OUT thectr INT)
BEGIN
SELECT organic from ctrs where ranking = theranking;
END $$
DELIMITER;
I call it like this:
CALL rankvolume(2,#foo)
It returns .125, which is correct. YAY!
But
SELECT #foo
Returns NULL.
Booo.
I've tried renaming all variables. I double-checked that I'm not reusing any variable names.
What am I missing?
you need to add a value to thectr
DELIMITER $$
DROP PROCEDURE IF EXISTS rankvolume $$
CREATE PROCEDURE `rankvolume`(IN theranking INT, OUT thectr INT)
BEGIN
SELECT organic INTO thectr from ctrs where ranking = theranking;
END $$
DELIMITER ;
db<>fiddle here

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 ;

Stored Procedure using Select Error 1064

I have a simple procedure to select data from an column and show them
DELIMITER //
DROP PROCEDURE IF EXISTS `testing`
CREATE PROCEDURE `testing`(IN param1 VARCHAR(40))
BEGIN
SELECT * FROM testingdatabase,
END //
DELIMITER;
However I keep getting error 1064
SELECT * FROM testingdatabase;
use ; not ,
So it should be:
DELIMITER //
DROP PROCEDURE IF EXISTS `testing`//
CREATE PROCEDURE `testing`(IN param1 VARCHAR(40))
BEGIN
SELECT * FROM testingdatabase;
END //
DELIMITER ;
DELIMITER //
DROP PROCEDURE IF EXISTS `testing`//
CREATE PROCEDURE `testing`(IN param1 VARCHAR(40))
BEGIN
SELECT * FROM testingdatabase;
END //
DELIMITER ;
DELIMITER //
DROP PROCEDURE IF EXISTS testing//
CREATE PROCEDURE testing(IN param1 VARCHAR(40)) BEGIN
SELECT * FROM testingdatabase;
END //
DELIMITER ;
Check this
DELIMITER $$
CREATE
PROCEDURE `testing`(IN param1 VARCHAR(40))
BEGIN
SELECT * FROM `test_table` WHERE `test_cdeo` LIKE param1;
END$$
DELIMITER ;
You can call and check your procedure via sql command like below code
CALL testing('149514')

IN() not working in mysql stored procedure

DELIMITER $$
USE `employee`$$
DROP PROCEDURE IF EXISTS `selectemployeebyids`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `selectemployeebyids`(IN empid VARCHAR(100))
BEGIN
SELECT empname,salary,department from emp where empid IN (empid) ORDER BY empid;
END$$
DELIMITER ;
can anyone please help me
With Regards
Manju K
After trying lot i find instead of IN(), FIND_IN_SET() works for me.
DELIMITER $$
USE `employee`$$
DROP PROCEDURE IF EXISTS `selectemployeebyids`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `selectemployeebyids`(IN empid VARCHAR(100))
BEGIN
SELECT empname,salary,department from emp where empid FIND_IN_SET(empid) ORDER BY empid;
END$$
DELIMITER ;
Try this
DELIMITER $$
USE `employee`$$
DROP PROCEDURE IF EXISTS `selectemployeebyids`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `selectemployeebyids`(IN empid_in VARCHAR(100))
BEGIN
SELECT empname,salary,department from emp
where concat(',',empid_in,',') like concat('%,'cast (empid as varchar(10)),',%')
ORDER BY empid;
END$$
DELIMITER ;

mysql stored procedure

I need to echo a statement in stored procedure
DELIMITER $$
DROP PROCEDURE IF EXISTS `Edit_table` $$
CREATE PROCEDURE `Edit_table` (in_db_nm varchar(20),in_tbl_nm
varchar(20),in_your_query varchar(200)) DETERMINISTIC BEGIN
select concat('Table ',in_tbl_nm, ' not found');
END $$
DELIMITER ;
this is what I get from the console when running it, it seems to always print the first line because that's the table column name, is there a way to remove this?
concat('Table ',in_tbl_nm, ' not found')
Table xxxxx not found
DELIMITER $$
DROP PROCEDURE IF EXISTS Edit_table $$
CREATE PROCEDURE Edit_table (in_db_nm varchar(20),in_tbl_nm
varchar(20),in_your_query varchar(200))DETERMINISTICBEGIN
select concat('Table ',in_tbl_nm, ' not found') as DisplayData;
END $$
DELIMITER ;
This changes the header to 'DisplayData'
You can also start MySQL with --skip-column-names option to hide the column names