IN() not working in mysql stored procedure - mysql

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 ;

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

How to fetch new columns from a table in a procedure in mysql?

delimiter $$
create procedure getcstatus(vuser_name varchar(20))
begin
select
a.c_id, a.c_name,
vga.* from a_c a
inner join v_getalla vga on a.a_id=vga.a_id
where a.c_name=vuser_name
group by vga.a_id, vga.a_name, vga.c_name, vga.s_f_id, vga.s_id, s_name, vga.developer_tool_filter_id;
end$$
delimiter ;
In this procedure I want to fetch 4 more columns from a different table how I can fetch it?
no need to use group by since you are not using any aggregation functions.
delimiter $$
create procedure getcstatus(vuser_name varchar(20))
begin
select *
from a_c a
inner join v_getalla vga on a.a_id=vga.a_id
where a.c_name=vuser_name
end$$
delimiter ;

how to pass parameter to stored procedure using 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);

mysql stored procedure error: missing semicolon

I have following store procedure. It is give me some error
DROP procedure IF exists getQueueMessage;
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `getQueueMessage`(msg varchar(100))
BEGIN
SELECT `Name` FROM queues WHERE Id IN (
SELECT PhysicalQueueId FROM indexqueuemaps WHERE ConditionFieldValue = msg)
END
END$$
DELIMITER ;
It is giving me missing semicolon error. Don't know Why this error is getting. Can someone help me?
Try like this:
DROP procedure IF exists getQueueMessage;
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `getQueueMessage`(msg varchar(100))
BEGIN
SELECT `Name` FROM queues WHERE Id IN (
SELECT PhysicalQueueId FROM indexqueuemaps WHERE ConditionFieldValue = msg);
END$$
DELIMITER ;
There's only one BEGIN and two ENDs, remove the 2nd END and you should be fine.
Replace root#localhost with root#localhost

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