动态SQL存储过程
(1)需求:根据传入参数是否为空或空字符串来拼接where条件的and语句,实现动态查询功能(不能使用MyBatis)
(2)解决方案:使用if判断语句实现
2.1存储过程 bdc_fwsyq 如下:(不同的mysql客户端写法略有差异,但原理相同,我这边是用的SQLyog)
DELIMITER $$ CREATE DEFINER=`root`@`59.213.105.24` PROCEDURE `bdc_fwsyq`( IN `in_bdcdzh` VARCHAR(255), IN `in_qlrzjbh` VARCHAR(255), IN `in_bdcqzh` VARCHAR(255) ) BEGIN SELECT * FROM bdc_regn_fwsyq WHERE FWSUOYQLR = IF(`in_bdcdzh` IS NULL OR `in_bdcdzh` = '',FWSUOYQLR,`in_bdcdzh`) AND QLRZJBH = IF(`in_qlrzjbh` IS NULL OR `in_qlrzjbh` = '',QLRZJBH,`in_qlrzjbh`) AND BDCQZH = IF(`in_bdcqzh` IS NULL OR `in_bdcqzh` = '',BDCQZH,`in_bdcqzh`) LIMIT 100; END$$ DELIMITER ;
2.2存储过程 bdc_fwsyq 调用:
CALL bdc_fwsyq("xx",null,"1111");
2.3以上调用最后执行的SQL语句如下:(实际需要执行的SQL语句可以自行调整存储过程来修改)
SELECT * FROM bdc_regn_fwsyq WHERE FWSUOYQLR = "xx" AND QLRZJBH = QLRZJBH AND BDCQZH = "1111" LIMIT 100;