forked from Thomas-S-B/SQLServerTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChangeOptionsInDatabases.sql
48 lines (40 loc) · 991 Bytes
/
ChangeOptionsInDatabases.sql
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
--Example: Sets AUTO_CLOSE to OFF on all databases with AUTO_CLOSE = ON
DECLARE @cursor_DBs CURSOR
DECLARE @MyDBs VARCHAR(500)
DECLARE @error_msg VARCHAR(500)
DECLARE @MyErrors TABLE
(myErrorMessage VARCHAR(1000)
)
DECLARE @SQL VARCHAR(8000)
--Create cursor over all relevant databases
SET
@cursor_DBs = CURSOR FOR
SELECT d.name
FROM sys.databases AS d
WHERE d.is_auto_close_on = 1
--Execute SQL on all databases
OPEN @cursor_DBs
FETCH NEXT
FROM @cursor_DBs INTO @MyDBs
WHILE @@FETCH_STATUS = 0
BEGIN
BEGIN TRY
SET @SQL = ''
SELECT @SQL = @SQL + 'ALTER DATABASE ' + @MyDBs
+ ' SET AUTO_CLOSE OFF;'
EXEC(@SQL)
END TRY
BEGIN CATCH
--There was an error and log it
INSERT INTO @MyErrors
VALUES (@MyDBs + ' -- ' + ERROR_MESSAGE())
END CATCH
FETCH NEXT
FROM @cursor_DBs INTO @MyDBs
END
SELECT *
FROM @MyErrors AS me
ORDER BY me.myErrorMessage
--Cleanup
CLOSE @cursor_DBs
DEALLOCATE @cursor_DBs