0
点赞
收藏
分享

微信扫一扫

mysql存储过程中根据传入参数,动态拼sql语句并且执行


CREATE DEFINER = 'root'@'%'
PROCEDURE databaseTest.myProcTest(IN i_start_index INT, IN i_capacity INT, IN i_type INT, IN i_category INT)
BEGIN
IF i_type = 0 THEN
set @sqlType= '';
ELSEIF i_type = 1 THEN
set @sqlType= ' AND hsr.type = 1';
END IF;

IF i_category = 0 THEN
set @sqlCategory = '';
ELSE
set @sqlCategory = CONCAT(' AND hsr.category_type = ', i_category);
END IF;

set @sql0 = 'SELECT
*
FROM
table_h_s_r hsr
WHERE hsr.auction_status = 1 ';

set @sql1 = CONCAT(' order BY hsr.sort_index asc LIMIT ',i_start_index, ' , ' , i_capacity);

set @finalSql = CONCAT(@sql0,@sqlType,@sqlCategory,@sql1);

PREPARE stmt FROM @finalSql;

EXECUTE stmt;

END


举报

相关推荐

0 条评论