这个MySQL脚本有什么不对吗?

时间:2010-06-04 22:04:01

标签: mysql mysql-error-1064

SELECT @cnt := COUNT(table_name) 
  FROM information_schema.tables 
 WHERE table_schema = 'TAA' 
   and table_name = 'Clients';

IF (@cnt > 0) THEN
  INSERT INTO `ClientsBAK` SELECT * FROM `Clients`;
END IF;

我在IF(@cnt > 1) THEN错误1064

时收到语法错误

2 个答案:

答案 0 :(得分:3)

AFAIK,你只能在例程(函数,触发器,存储过程)中使用IF。

答案 1 :(得分:1)

创建存储过程:

DELIMITER //
CREATE PROCEDURE BackupClients()
BEGIN

    SELECT @cnt := COUNT(table_name)
    FROM information_schema.tables
    WHERE table_schema = 'TAA'
    AND table_name = 'Clients';

    IF @cnt > 0 THEN
        INSERT INTO `ClientsBAK` SELECT * FROM `Clients`;
    END IF;

END //
DELIMITER ;

CALL BackupClients();

我认为你也想要IF @cnt > 0而不是IF @cnt > 1,所以我为你改变了。