开发者

Whats Wrong with this MySQL Script? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 9 years ago.
SELECT @cnt := COUNT(table_name) 
  FROM information_schema.tables 
 WHERE t开发者_JAVA技巧able_schema = 'TAA' 
   and table_name = 'Clients';

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

I get a syntax error at IF(@cnt > 1) THEN error 1064


AFAIK, you can only use IF like that in routines (functions, triggers, stored procedures).


Create a stored procedure:

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();

I think you also want IF @cnt > 0 instead of IF @cnt > 1, so I changed that for you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜