MySql动态拼接SQL并动态赋值

CREATE DEFINER=`root`@`localhost` PROCEDURE `p_dc_log`(
IN UName VARCHAR(50),
IN Description VARCHAR(50),
IN PageSize int,
IN PageIndex int,
OUT TotalCount int
)
BEGIN
    set @QueryWhere = ‘‘;
    
    #条件查询
    IF LENGTH(UName) > 0 THEN
      set @QueryWhere = CONCAT(@QueryWhere, AND UserName = ? );
    END IF;
    
    IF LENGTH(Description) > 0  THEN
        set @QueryWhere = CONCAT(@QueryWhere, AND Description LIKE ‘‘%,Description,%‘‘ );
    END IF;
    
    #查询总条数
    SELECT @QuerySql := CONCAT(SELECT COUNT(1) INTO @TotalCount FROM dc_log WHERE 1 = 1,@QueryWhere);

    PREPARE stmt FROM SELECT COUNT(1) INTO @TotalCount FROM dc_log WHERE 1 = 1;
    
    EXECUTE stmt;    
    
    SELECT @TotalCount AS Tcount;
    
    SET TotalCount = @TotalCount;
END

 

MySql动态拼接SQL并动态赋值

上一篇:MongooooooooooooooooooooDB入门三:MongoDB shell


下一篇:Oracle数据迁移后由列的直方图统计信息引起的执行计划异常