我使用了下面的存储过程来动态获取表名,并获得搜索关键字。
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetTableLength`(IN TABLE_NAME VARCHAR(100),
IN SEARCH VARCHAR(150)) BEGIN IF (SEARCH = '') THEN
SET @query = CONCAT('select count(*) as total from ',TABLE_NAME); ELSE
SET @query = CONCAT('select count(userId) as total from ',TABLE_NAME, ' WHERE userName like ', SEARCH); END IF; PREPARE stmt3
FROM @query; EXECUTE stm3; END
当我打电话给上面的程序时如下。
CALL GetTableLength('usertbl', 'kisan')
此处,kisan是搜索关键字。
然后发生以下错误。怎么了?
错误代码:1054。'where子句'中的未知列'kisan'
答案 0 :(得分:0)
尝试使用如下:
set @query = CONCAT("select count(userId) as total from ",table_name, " WHERE userName like '", search,"'");
即使您可以尝试:
set @query = CONCAT("select count(userId) as total from ",table_name, " WHERE userName like '%", search,"%'");
答案 1 :(得分:0)
您需要使用引用''