如果ELSE在Mysql中失败

时间:2015-10-28 19:08:39

标签: mysql stored-procedures

我在过程中有一个简单的IF ELSE但如果执行它会出现以下错误:

  

的MySQL>拨打daily_qip; ERROR 1295(HY000):此命令不是   在准备好的声明协议中支持

DELIMITER |
DROP PROCEDURE IF EXISTS daily_qip |
CREATE  PROCEDURE daily_qip()
BEGIN
DECLARE BlockExist INT ;
SELECT COUNT(1) INTO BlockExist FROM information_schema.tables WHERE table_schema = "GEO" AND table_name = "blocks";
IF BlockExist = 0 THEN 
    SET @BlocksTable = CONCAT('blocks_', DATE_FORMAT(CURDATE(),'%Y%m%d') );
    SET @blocks_view = CONCAT('CREATE VIEW blocks AS',' SELECT * FROM ',@BlocksTable);
    PREPARE stmt from @blocks_view;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
ELSE
    SET @BlocksTable = CONCAT('blocks_', DATE_FORMAT(CURDATE(),'%Y%m%d') );
    SET @blocks_view = CONCAT('ALTER VIEW blocks AS',' SELECT * FROM ',@BlocksTable);
    PREPARE stmt from @blocks_view;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
END IF;
END;
|
DELIMITER ;

任何指针都会有很大的帮助

1 个答案:

答案 0 :(得分:1)

ALTER VIEW无法通过准备好的声明完成。有关预准备语句中允许的查询列表,请参阅SQL Syntax Allowed in Prepared Statements

CREATE VIEWDROP VIEW都是允许的。因此,不要改变视图,先将其删除,然后重新创建它。

DELIMITER |
DROP PROCEDURE IF EXISTS daily_qip |
CREATE  PROCEDURE daily_qip()
BEGIN
    DROP VIEW IF EXISTS blocks;
    SET @BlocksTable = CONCAT('blocks_', DATE_FORMAT(CURDATE(),'%Y%m%d') );
    SET @blocks_view = CONCAT('CREATE VIEW blocks AS',' SELECT * FROM ',@BlocksTable);
    PREPARE stmt from @blocks_view;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
END;
|
DELIMITER ;