我有一個刪除所有表的腳本。在存儲過程中檢查var爲NULL
SET FOREIGN_KEY_CHECKS = 0;
SET @tables = NULL;
SELECT GROUP_CONCAT(table_schema, '.', table_name) INTO @tables
FROM information_schema.tables
WHERE table_schema = 'mydb';
SET @tables = CONCAT('DROP TABLE ', @tables);
PREPARE stmt FROM @tables;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET FOREIGN_KEY_CHECKS = 1;
它完美地工作只有一次 - 當至少有一個表存在。 第二輪給我一個錯誤:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NULL' at line 1 SQL.sql 9 1
我想IF語句中使用,但它仍然不能正常工作:
IF @tables IS NOT NULL THEN
SET @tables = CONCAT('DROP TABLE ', @tables);
PREPARE stmt FROM @tables;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END IF;
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'if @tables IS NOT NULL THEN SET @tables = CONCAT('DROP TABLE ', @tables)' at line 1 SQL.sql 8 1
什麼是使用IF正確的方法這個案例?