Whats Wrong with this MySQL Script? [closed]
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.
精彩评论