views:

46

answers:

2
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;

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

+3  A: 

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

Wrikken
+1  A: 

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.

Mark Byers
Correct- I had been playing with the code and left that typo in.
manyxcxi