1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SQLSERVER 複数DBを一括AUTO_CLOSE offにする

Last updated at Posted at 2019-04-23

db_auto_close.sql

declare @tbl table (
  rowid int IDENTITY(1,1), 
  dname varchar(100)
)
declare @i int, @max int, @dname varchar(100), @SQL VARCHAR(1000);

insert into @tbl(dname)
select a.name
from master.sys.databases as a
WHERE a.name NOT IN ('master', 'tempdb', 'model', 'msdb')
 and a.name not like 'ReportServer%'
 and a.is_auto_close_on = 1

set @i = 1
select @max = max(rowid) from @tbl

WHILE (@i <= @max)
BEGIN
  select @dname = a.dname
  from @tbl as a
  where a.rowid = @i

  SET @SQL = 'ALTER DATABASE ' + @dname + ' set AUTO_CLOSE off';
  exec(@SQL);

  set @i += 1

end
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?