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. –
…in example I have removed ‘harga’ field from the list of fields; add it if you need.