Advertisement
Guest User

BugReport-Mysql-Event-NoData

a guest
Apr 13th, 2011
416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 1.58 KB | None | 0 0
  1. DELIMITER ;;
  2.  
  3. CREATE PROCEDURE `FixTable`(IN `TableName` VARCHAR (400), IN `FixLevel` INT)
  4.  
  5. BEGIN
  6.  
  7.   declare done int default 0;
  8.   DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;
  9.  
  10.   CASE FixLevel
  11.     WHEN 1 THEN SET @SQLText = CONCAT('REPAIR TABLE `',TableName,  '` QUICK');
  12.     WHEN 2 THEN SET @SQLText = CONCAT('REPAIR TABLE `',TableName,  '` EXTENDED');
  13.     WHEN 3 THEN SET @SQLText = CONCAT('REPAIR TABLE `',TableName,  '` USE_FRM');
  14.     WHEN 4 THEN SET @SQLText = CONCAT('OPTIMIZE LOCAL TABLE `',TableName,  '` ');
  15.   END CASE;
  16.   PREPARE RES from @SQLText;
  17.   EXECUTE RES;
  18. END ;;
  19.  
  20. DELIMITER ;
  21.  
  22.  
  23.  
  24.  
  25. DELIMITER ;;
  26.  
  27. CREATE PROCEDURE `fixAllTables`(IN `FixLevel` INT)
  28.  
  29. begin
  30.  
  31.   DECLARE done INT DEFAULT 0;
  32.  
  33.   declare TableName VARCHAR(30);
  34.  
  35.   DECLARE tablestatusCur CURSOR FOR
  36.  
  37.   SELECT TABLE_NAME FROM information_schema.`TABLES` T
  38.     where TABLE_SCHEMA=SCHEMA() and Table_comment not like '%VIEW%' and Table_ROWS is null;
  39.  
  40.  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
  41.  
  42.  
  43.  
  44.  open tablestatusCur;
  45.  
  46.  repeat
  47.  
  48.    FETCH tablestatusCur INTO TableName;
  49.  
  50.    if not done then
  51.  
  52.      call FixTable(TableName,FixLevel);
  53.  
  54.    end if;
  55.  
  56.  until done end repeat;
  57.  
  58. end ;;
  59.  
  60. DELIMITER ;
  61.  
  62. DELIMITER ;;
  63. CREATE EVENT `fixTablesHourly`
  64.     ON SCHEDULE EVERY 1 HOUR STARTS CURRENT_TIMESTAMP
  65.     ON COMPLETION NOT PRESERVE
  66.     ENABLE
  67.     COMMENT 'Check for damaged tables and fix them.'
  68. DO BEGIN
  69.     DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET @LastEventErrorTime=CURRENT_TIMESTAMP;
  70.     call fixAllTables(1);
  71.     call fixAllTables(2);
  72.     call fixAllTables(3);
  73. END  ;;
  74.  
  75.  
  76. DELIMITER ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement