2

6か月以上前に作成されたデータベースのリストを次のように取得できます:-

-- all databases over 6 months old
select name, crdate
from sys.sysdatabases
where crdate <= DATEADD(month, -6, GETDATE())
      AND name not in ('master','model','msdb','tempdb','distribution')

次のような結果が得られます:-

name        crdate
db1         2008-06-25 09:01:11.747
db2         2008-06-25 09:01:50.967

私はこのようにデータベースを切り離すことができます:-

-- detach database
EXEC master.dbo.sp_detach_db @dbname = N'db1',
@keepfulltextindexfile = N'true'

sp_detach_db最初のクエリが返すデータベースごとに実行する必要があります。

これを行うための最良の方法は何ですか?

4

1 に答える 1

3

タスクにカーソルを使用できます。

declare cur cursor for
select name
from sys.sysdatabases
where crdate <= DATEADD(month, -6, GETDATE())
and name not in ('master','model','msdb','tempdb','distribution')

declare @name nvarchar(200)

open cur

fetch next from cur into @name

while @@FETCH_STATUS = 0
begin
    EXEC master.dbo.sp_detach_db @dbname = @name, @keepfulltextindexfile = N'true'
    fetch next from cur into @name
end

close cur
deallocate cur      
于 2013-01-09T11:23:18.363 に答える