begin
set @sql=concat('select A from tb');
    if ste='1' then
     set @sql=concat('select A from tb','where aa='xxx'');
   elseif ste='0' then
     set @sql=concat('select A from tb','where bb='yyy'');
  end if
    prepare stml from @sql;
execute stml;
    deallocate prepare stmt;
end

解决方案 »

  1.   

    DELIMITER ##
    CREATE PROCEDURE TEST(STE CHAR(1))
    begin
     set @sql=concat('select A from tb');
         if ste='1' then
          set @sql=concat('select A from tb','where aa='xxx'');
        elseif ste='0' then
          set @sql=concat('select A from tb','where bb='yyy'');
       end if
         prepare stml from @sql;
     execute stml;
         deallocate prepare stmt;
     end ##
    DELIMITER ;
    CALL TEST('1')
      

  2.   

    DELIMITER ##
     CREATE PROCEDURE TEST(STE CHAR(1))
     BEGIN
      SET @SQL=CONCAT('select A from tb');
          IF ste='1' THEN
           SET @SQL=CONCAT('select A from tb','where aa=\'xxx');
         ELSEIF ste='0' THEN
           SET @SQL=CONCAT('select A from tb','where bb=\'yyy');
        END IF;
          PREPARE stml FROM @SQL;
      EXECUTE stml;
          DEALLOCATE PREPARE stmt;
      END ##
     DELIMITER ;