how to pass varchar string to stored procedure where in condition? - mysql

how to pass varchar string to stored procedure where in condition?
here, where in condition not work how to solve this problem?
CALL searchStudentByName("%AAP%","'1','2','3'");
MYSQL Stored Procedures:
DELIMITER $$
USE `studentsdb`$$
DROP PROCEDURE IF EXISTS `searchStudentByName`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `searchStudentByName`(IN `search_key` VARCHAR(40),IN `student_ids` VARCHAR(350))
BEGIN
SELECT students.StudentId,students.StudentName FROM students WHERE students.Status='A' AND students.StudentName LIKE search_key AND students.StudentId NOT IN(student_ids) LIMIT 5;
END$$
DELIMITER ;

You really need to prepare a statement to use a list of values like that. Try this:
CREATE DEFINER=`root`#`localhost` PROCEDURE `searchStudentByName`(IN `search_key` VARCHAR(40),IN `student_ids` VARCHAR(350))
BEGIN
SET #sql = CONCAT("SELECT students.StudentId,students.StudentName FROM students WHERE students.Status='A' AND students.StudentName LIKE '", search_key, "' AND students.StudentId NOT IN(", student_ids, ") LIMIT 5");
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END$$

Related

How can I send a table as a parameter to stored procedure in MySQL?

I have a stored procedure in MySQL like:
DELIMITER $$
CREATE PROCEDURE addIns(IN insCode VARCHAR(21), IN insName VARCHAR(21), IN stat BIT(1))
BEGIN
INSERT INTO institution(institution.institution_code, institution.institution_name, institution.is_active)
VALUES (insCode, insName, stat);
END$$
DELIMITER ;
But I want like this:
DELIMITER $$
CREATE PROCEDURE addIns(IN _institution)
BEGIN
INSERT INTO institution(institution.institution_code, institution.institution_name, institution.is_active)
VALUES (_institution.institution_code, _institution.institution_name, _institution.is_active);
END$$
DELIMITER ;
Because I want to use this stored procedure in Java Spring App.
How can I provide it?
My instutuion table is:
CREATE PROCEDURE addIns(IN _institution)
BEGIN
#sql := CONCAT( 'INSERT INTO institution (institution_code, institution_name, is_active) SELECT institution_code, institution_name, is_active FROM ' , _institution);
PREPARE stmt FROM #sql;
EXECUTE stmt;
DROP PREPARE stmt;
END

MYSQL : Undeclared variable

I have two stored procedure the first parameter with static and it works well
but when I but dynamic parameter it gives me an error Undeclared variable: limitvar
1/procedure
`DELIMITER $$
DROP PROCEDURE IF EXISTS `gestprospects`.`recClientsFiltre` $$
CREATE PROCEDURE `recClientsFiltre`(IN params text, in limitvar int , in offsetvar int )
BEGIN
SET #query := concat(concat("SELECT ... WHERE CONCAT(clients.id,'-') in (",params),") limit 10,10");
PREPARE stmt FROM #query ;
EXECUTE stmt ;
DEALLOCATE PREPARE stmt;
END $$
DELIMITER ;`
2/procedure
DELIMITER $$
DROP PROCEDURE IF EXISTS `gestprospects`.`recClientsFiltre` $$
CREATE PROCEDURE `recClientsFiltre`(IN params text, in limitvar int , in offsetvar int )
BEGIN
SET #query := concat(concat("SELECT ... WHERE CONCAT(clients.id,'-') in (",params),") limit limitvar,offsetvar");
PREPARE stmt FROM #query ;
EXECUTE stmt ;
DEALLOCATE PREPARE stmt;
END $$
DELIMITER ;
Thank you for your reply
You are including the litteral strings limitvar and offsetvar in your dynamic query, whereas you want to include their value.
#query := CONCAT(
"SELECT ... WHERE CONCAT(clients.id,'-') IN (", params,")",
" LIMIT ", limitvar, ", ", offsetvar);
Notice how you can concatenate several strings in one call to CONCAT();

MySQL stored procedures - How to use parameter in FROM clause

I have the following stored procedure:
DELIMITER ///
CREATE PROCEDURE tmp_test_proc(__TABLE__NAME varchar(255))
BEGIN
SELECT COUNT(*) FROM __TABLE__NAME;
END///
DELIMITER ;
I would like to select from the parameter __TABLE__NAME, but MySQL tells me there is no table __TABLE__NAME... So is there a way to use the value of the parameter in the from clause?
you cannot parameterized table names (as well as column names) by default, you need to create PreparedStatement for this,
DELIMITER ///
CREATE PROCEDURE tmp_test_proc(__TABLE__NAME varchar(255))
BEGIN
SET #sql = CONCAT('SELECT COUNT(*) FROM ', __TABLE__NAME);
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END///
DELIMITER ;

MySQL Stored Procedure In Query

I want to make a MySQL Stored Procedure
DELIMITER $$
DROP PROCEDURE IF EXISTS `SECTOR_INDUSTRY_SP` $$
CREATE PROCEDURE `SECTOR_INDUSTRY_SP`(IN RINSERIES TEXT, IN COMMUNITYIDS TEXT)
READS SQL DATA
BEGIN
SELECT *
from PROFESSIONAL_IDENTITY_MERGED p , std_company_detail s
where (p.pim_company_id = s.cid) AND p.pim_community_id IN ('+'+COMMUNITYIDS+'+')
AND p.pim_rin IN ('+'+RINSERIES+'+');
END $$
DELIMITER ;
As you can see I have two arguments to call this procedure RINSERIES which would be text format like '12312,1234,1239' similarly COMMUNITYIDS is like '2,5,8' but When I would pass, it is interpreting like
SELECT *
from PROFESSIONAL_IDENTITY_MERGED p , std_company_detail s
where (p.pim_company_id = s.cid) AND p.pim_community_id IN ('12312,1234,1239')
AND p.pim_rin IN ('2,5,8');
But I want this query to look like following
SELECT *
from PROFESSIONAL_IDENTITY_MERGED p , std_company_detail s
where (p.pim_company_id = s.cid) AND p.pim_community_id IN (12312,1234,1239)
AND p.pim_rin IN (2,5,8);
How can I do this? Thanks!
Try this:
DELIMITER $$
DROP PROCEDURE IF EXISTS `SECTOR_INDUSTRY_SP` $$
CREATE PROCEDURE `SECTOR_INDUSTRY_SP`(IN RINSERIES TEXT, IN COMMUNITYIDS TEXT)
READS SQL DATA
BEGIN
SET #s = CONCAT("SELECT *
from PROFESSIONAL_IDENTITY_MERGED p , std_company_detail s
where (p.pim_company_id = s.cid) AND p.pim_community_id IN (", COMMUNITYIDS, ")
AND p.pim_rin IN (",RINSERIES,")");
PREPARE stmt FROM #s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END $$
DELIMITER ;

How to send table name as Mysql Store procedure

I write a store procedure.But it don't take the table name as a parameter.Now how i send a table name as aparameter.Pls see my proc below:
DELIMITER $$
USE `db_test`$$
DROP PROCEDURE IF EXISTS `test_proc`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `test_proc`(IN newsInfoTable VARCHAR(100))
BEGIN
SELECT news INTO #news
FROM newsInfoTable
WHERE CURDATE()=DATE_FORMAT(date_time,'%Y-%m-%d')
ORDER BY date_time DESC LIMIT 1;
SELECT #news;
END$$
DELIMITER ;
Calling parameter:
USE db_test;
CALL test_proc('tbl_news_list');
But the ERROR is: Table 'db_test.newsinfotable' doesn't exist
How solve this problem.Pls help.
Use prepared statements in your procedure body:
SET #s = CONCAT('SELECT ... FROM ', newsInfoTable);
PREPARE stmt FROM #s;
EXECUTE stmt;
//.....
DEALLOCATE PREPARE stmt;
Your code should look like :
SET #sql_stam = CONCAT('SELECT news INTO #news FROM ',newsInfoTable,
' WHERE DATE_FORMAT(date_time,\'%Y-%m-%d\')
ORDER BY date_time DESC LIMIT 1');
...
SELECT #news;
Also, I don't see any reasons you need to use #news variable...