mysql动态传参
drop PROCEDURE if exists my_pro;
create PROCEDURE my_pro(in namea varchar(50),in b varchar(100))
BEGIN
SET @selectSql = 'SELECT id,name,type from user ';
IF(namea<>'')THEN
SET @selectSql =CONCAT(@selectSql,' where name like ',"'%",namea,"%'");
END IF;
IF(b<>'')THEN
SET @selectSql =CONCAT(@selectSql,' and type in ',"(",b ,")");
END IF;
SET @selectSql =CONCAT(@selectSql,' order by id desc');
-- SELECT @selectSql; -- 打印sql语句
PREPARE stmt FROM @selectSql;
EXECUTE stmt ;
DEALLOCATE PREPARE stmt;
end;
call my_pro('zhang','"123","12"');
结果如下: