LoginSignup
0
1

More than 5 years have passed since last update.

sqlserverでテーブルを全部削除するスクリプト

Last updated at Posted at 2017-08-15

制約を削除してから、テーブル削除する

-- 制約を全て削除する
DECLARE @sql NVARCHAR(MAX) = N''

SELECT @sql = @sql + N'
  ALTER TABLE ' + QUOTENAME(s.name) + N'.'
  + QUOTENAME(t.name) + N' DROP CONSTRAINT '
  + QUOTENAME(c.name) + ';'
FROM sys.objects AS c
INNER JOIN sys.tables AS t
ON c.parent_object_id = t.[object_id]
INNER JOIN sys.schemas AS s
ON t.[schema_id] = s.[schema_id]
WHERE c.[type] IN ('D','C','F','PK','UQ')
ORDER BY c.[type]

-- PRINT @sql
EXEC @sql

-- 全テーブルを削除する
EXEC sp_MSforeachtable @command1 = "DROP TABLE dbo.?";

COMMIT;
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1