forked from MichelleUfford/sql-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdba_recompile_sp.sql
More file actions
123 lines (93 loc) · 3.86 KB
/
dba_recompile_sp.sql
File metadata and controls
123 lines (93 loc) · 3.86 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
Use dbaTools;
Go
If ObjectProperty(Object_ID('dbo.dba_recompile_sp'), N'IsProcedure') Is Null
Begin
Execute ('Create Procedure dbo.dba_recompile_sp As Print ''Hello World!''')
RaisError('Procedure dba_recompile_sp created.', 10, 1);
End;
Go
Set ANSI_Nulls On;
Set Quoted_Identifier On;
Go
Alter Procedure dbo.dba_recompile_sp
/* Declare Parameters */
@databaseName nvarchar(128) = Null /* Null = all databases */
, @tableName nvarchar(128) = Null /* Null = all tables */
As
/**********************************************************************************************************
NAME: dba_recompile_sp
SYNOPSIS: Recompiles all procs in a specific database or all procs; can recompile a specific table, too.
DEPENDENCIES: The following dependencies are required to execute this script:
- SQL Server 2005 or newer
AUTHOR: Michelle Ufford, http://sqlfool.com
CREATED: 2009-09-12
VERSION: 1.0
LICENSE: Apache License v2
----------------------------------------------------------------------------
DISCLAIMER:
This code and information are provided "AS IS" without warranty of any kind,
either expressed or implied, including but not limited to the implied
warranties or merchantability and/or fitness for a particular purpose.
----------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
-- DATE VERSION AUTHOR DESCRIPTION --
---------------------------------------------------------------------------------------------------------
20150619 1.0 Michelle Ufford Open Sourced on GitHub
**********************************************************************************************************/
Set NoCount On;
Set XACT_Abort On;
Set Ansi_Padding On;
Set Ansi_Warnings On;
Set ArithAbort On;
Set Concat_Null_Yields_Null On;
Set Numeric_RoundAbort Off;
Begin
/* Make sure the global temp tables do not already exist, i.e. failed execution */
If Exists(Select * From tempdb.sys.tables Where name = '###databaseList')
Drop Table #databaseList;
If Exists(Select * From tempdb.sys.tables Where name = '##tableList')
Drop Table tableList;
/* Declare Temp Tables */
Create Table ##databaseList
(
databaseName nvarchar(128)
, processed bit
);
Create Table ##tableList
(
databaseName nvarchar(128)
, tableName nvarchar(128)
, processed bit
);
Insert Into ##databaseList
Select name As databaseName
, 0 As processed
From sys.databases
Where name = IsNull(@databaseName, name);
While Exists(Select Top 1 databaseName From ##databaseList Where processed = 0)
Begin
Execute sp_msforeachdb 'Use ?;
Select name As tableName
Into ##tableList
From sys.tables
Where name = IsNull(@tableName, name);
Declare @tableName nvarchar(128) = (Select Top 1 tableName From #tableList);
While Exists(Select Top 1 * From #tableList)
Begin
Execute sp_recompile @tableName;
Delete From #tableList Where tableName = @tableName;
Select Top 1 @tableName = tableName From #tableList Order By tableName;
End;
Drop Table ##tableList;'
End
Set NoCount Off;
Return 0;
End
Go
Set Quoted_Identifier Off;
Go
If ObjectProperty(Object_ID('dbo.dba_recompile_sp'), N'IsProcedure') = 1
RaisError('Procedure dba_recompile_sp was successfully updated.', 10, 1);
Else
RaisError('Procedure dba_recompile_sp FAILED to create!', 16, 1);
Go