序
還是整理記錄。
作法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| USE yourDataBaseName GO
DECLARE @table_schema varchar(100) ,@table_name varchar(100) ,@constraint_schema varchar(100) ,@constraint_name varchar(100) ,@cmd nvarchar(200)
DECLARE table_cursor CURSOR FOR select TABLE_SCHEMA, TABLE_NAME from INFORMATION_SCHEMA.TABLES where TABLE_NAME != 'sysdiagrams' OPEN table_cursor FETCH NEXT FROM table_cursor INTO @table_schema, @table_name WHILE @@FETCH_STATUS = 0 BEGIN SELECT @cmd = 'DROP TABLE [' + @table_schema + '].[' + @table_name + ']' EXEC sp_executesql @cmd FETCH NEXT FROM table_cursor INTO @table_schema, @table_name END CLOSE table_cursor DEALLOCATE table_cursor
|
補充說明一下,這版假如在移除有關聯資料表的時候,會因為 FOREIGN KEY constraint 而失敗,要先移除所有的關聯限制喔。
可以參考我這篇移除關聯限制。