mysql存储过程执行动态sql语句

CREATE
    /*[DEFINER = { user | CURRENT_USER }]*/
    PROCEDURE `student`.`select_view_score`(IN WhereCondition VARCHAR(500),IN OrderByExpress VARCHAR(100))
    /*LANGUAGE SQL
    | [NOT] DETERMINISTIC
    | { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }
    | SQL SECURITY { DEFINER | INVOKER }
    | COMMENT 'string'*/
    BEGIN
 DECLARE stmt VARCHAR(2000);
 IF LENGTH(OrderbyExpress)>0 THEN
 BEGIN
      SET @sqlstr=CONCAT('select *  from view_score where ',WhereCondition,' order by ',OrderByExpress);
 END;
 ELSE
 BEGIN
      SET @sqlstr=CONCAT('select *  from view_score where ',WhereCondition);
 END;
 END IF;
 PREPARE stmt FROM @sqlstr;
 EXECUTE stmt;
    END


调用

CALL select_view_score('成绩>2 and 姓名="李兴乐"',' ');

你可能感兴趣的:(mysql存储过程执行动态sql语句)