How to create a flexible MySQL search procedure? - mysql

Hello dearest community,
EDITED
This is my solution, based on Devart answer. I slightly modify the procedure parameter and also fix some % things..
DELIMITER $$
DROP PROCEDURE IF EXISTS `cosmedicdb`.`proc_searchall` $$
CREATE PROCEDURE `cosmedicdb`.`proc_searchall` (output TEXT, tbl varchar(50), kolom_kriteria VARCHAR(20),
kolom_nilai VARCHAR(20))
BEGIN
SET #query = CONCAT('SELECT ', output, ' FROM ',tbl,' WHERE ', kolom_kriteria, ' LIKE CONCAT(','\'%',kolom_nilai, '%\')');
PREPARE stmt FROM #query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END $$
DELIMITER ;
I think one can enhance this to support search of numeric value field
Original Question below
First of all, I hope this is not a repost question.
I want to create a flexible search procedure. That is, the column ad the value of the column to be search, can be supplied from the argument itself. Currently this is my search procedure looks like :
DELIMITER $$
DROP PROCEDURE IF EXISTS `cosmedicdb`.`proc_searchtindakan` $$
CREATE PROCEDURE `cosmedicdb`.`proc_searchtindakan` (kolom VARCHAR(20),
kolomnilai VARCHAR(20))
BEGIN
CASE kolom
WHEN 'jenis'
THEN
SELECT jenis, harga
FROM cosmedicdb.tb_mastertindakan
where jenis like concat('%',kolomnilai,'%');
END CASE;
END $$
DELIMITER ;
You can see that this procedure only work for certain column to be search. Is it possible (and safe) to create just one search procedure that allows me to search using any column as the defining search criteria?
Thanks

You can do it with a prepared statements: build a query and execute it, e.g. -
DELIMITER $$
CREATE PROCEDURE proc_searchtindakan(IN kolom VARCHAR(20), IN kolomnilai VARCHAR(20))
BEGIN
SET #query = CONCAT('SELECT ', kolom, 'FROM cosmedicdb.tb_mastertindakan WHERE ', kolom, ' LIKE CONCAT(\'%\'', kolomnilai, '\'%\'');
PREPARE stmt FROM #query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
$$
DELIMITER ;
...in example I have removed 'harga' field from the list of fields; add it if you need.

Related

Can i create a procedure or fuction to drop a parameter table in mysql?

delimiter //
DROP PROCEDURE IF EXISTS drop_table//
create procedure drop_table(in table_name varchar) #创建带参数的存储过程
begin
drop table if EXISTS table_name;
end//
delimiter ;
call drop_table('a')
i use Navicat to create a procedure to drop a parameter table in mysql.
Error:Invalid Stored Procedure Syntax
As commented by P.Salmon, you cannot pass substitute the procedure argument (ie a variable) directly in a sql statement. You need dynamic sql: build the query as a string, then execute it.
delimiter //
create procedure drop_table(in p_table_name varchar(200))
begin
set #q = concat('drop table if exists ', p_table_name);
prepare stmt from #q;
execute stmt;
deallocate prepare stmt;
end//
delimiter ;
Demo on DB Fiddle
Note: the varchar datatype needs a length.

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

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$$

MySQL Stored Procedure basics

This looks like a pretty rudimentary question but for someone who is new to MySQL, it has proven to be a tough nut to crack.
I've been trying to create a stored procedure in the MySQL and this is what I tried:
CREATE DEFINER=`root`#`localhost` PROCEDURE `test_procedure`(
IN dtst_nm varchar(42)
)
BEGIN
SELECT
COUNT(*)
FROM
data_hub.dtst_nm
;
END
When I run this, I get the error that dtst_nm doesn't exist. The exact error message is:
"Error Code: 1146. Table 'data_hub.dtst_nm' doesn't exist"
Clearly the variable is not getting resolved.
From what I gathered, the syntax seems to be right. What am I missing?
This is a Dynamic SQL problem. You cannot directly specify variables in place of table and column names. You will need to use string functions of create SQL query string. Then use Prepare with Execute to run the query.
Try:
DELIMITER $$
DROP PROCEDURE IF EXISTS `test_procedure` $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `test_procedure`(
IN `dtst_nm` varchar(42)
)
BEGIN
SET #s = CONCAT('SELECT COUNT(*) FROM ', dtst_nm );
PREPARE stmt FROM #s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END $$
DELIMITER ;

Can I use a String as a WHERE CLAUSE

Can anybody help me with this JSP problem.
Im trying to update the database using code similar to this:
So, I have this on my Servlet:
String QueryCondition = "id = 1";
That will be passed to this stored procedure:
CREATE
DEFINER=`root`#`localhost`
PROCEDURE `storedprocedure_1`(QueryCondition TEXT)
BEGIN
UPDATE users SET name = 'John'
WHERE QueryCondition;
END
I was thinking if this is possible because the update always fail.
If this isn't possible can you recommend how can i do such thing
You can use it in a stored procedure but with a prepared statement.
Example:
DELIMITER //
CREATE
DEFINER=root#localhost
PROCEDURE storedprocedure_1(QueryCondition TEXT)
BEGIN
SET #query := CONCAT( 'UPDATE users SET name = \'John\' WHERE ',
QueryCondition );
PREPARE stmt FROM #query;
EXECUTE stmt;
DROP PREPARE stmt;
END;
//
DELIMITER ;

MySQL Stored Procedure using IN variable name not value

I have the following stored procedure defined:
DROP PROCEDURE IF EXISTS `Create_Domain_Table`;
DELIMITER ;;
PROCEDURE `Create_Domain_Table`(IN newTable VARCHAR(255))
BEGIN
CREATE TABLE newTable LIKE `domain_template`;
END;;
DELIMITER ;
However when I call the procedure e.g.
CALL Create_Domain_Table('test_domain_table');
it creates a new table with the a name of newtable and not test_domain_table.
Is there something wrong with my syntax or am I referencing the parameter incorrectly?
Thanks
Use Dynamic SQL on this,
DROP PROCEDURE IF EXISTS `Create_Domain_Table`;
DELIMITER ;;
CREATE PROCEDURE `Create_Domain_Table`(IN newTable VARCHAR(255))
BEGIN
SET #sql = CONCAT('CREATE TABLE `', newTable,'` LIKE domain_template');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END;;
DELIMITER ;