You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Utility Scripts/helper_script_for_adding_schemabinding_to_scalar_functions.sql
+50-23Lines changed: 50 additions & 23 deletions
Original file line number
Diff line number
Diff line change
@@ -9,9 +9,11 @@ The script ignores the following functions:
9
9
- Self-referencing functions.
10
10
- Functions that contain code that uses sp_executesql or OLE Automation procedures.
11
11
- Functions in schemas "sys" and "tSQLt".
12
+
- Functions that use OPENROWSET or OPENQUERY
12
13
- Functions dependent on synonyms (can be disabled by setting @IgnoreFunctionsDependentOnSynonyms to 0)
13
14
- Functions that have constraints dependent on them (can be disabled by setting @IgnoreFunctionsWithConstraintDependencies to 0)
14
15
- Functions dependent on linked servers (can be disabled by setting @IgnoreFunctionsDependentOnLinkedServers to 0)
16
+
- Functions dependent on objects in other databases (can be disabled by setting @IgnoreFunctionsWithCrossDatabaseDependencies to 0)
15
17
- Functions referencing functions fitting any of the above criteria (recursive).
16
18
17
19
Instructions:
@@ -33,6 +35,9 @@ DECLARE
33
35
,@IgnoreFunctionsDependentOnSynonyms BIT=1-- optionally filter out functions that depend on synonyms (cannot be schemabound)
34
36
,@IgnoreFunctionsWithConstraintDependencies BIT=1-- optionally filter out functions that have constraints dependant on them (cannot be altered)
35
37
,@IgnoreFunctionsDependentOnLinkedServers BIT=0-- optionally filter out functions that possibly depend on linked servers (cannot be schemabound)
38
+
,@IgnoreFunctionsWithTableValuedParameters BIT=1-- optionally filter out functions that have table-valued parameters (cannot be schemabound)
39
+
,@IgnoreFunctionsWithCrossDatabaseDependencies BIT=1-- optionally filter out functions that depend on objects in another database (cannot be schemabound)
40
+
,@EnableRecursiveCheck BIT=1-- optionally perform recursive exclusions (i.e. exclude functions that depend on excluded functions). Beware of infinite recursion here!
36
41
37
42
SETTRANSACTIONISOLATIONLEVELREADUNCOMMITTED;
38
43
SETNOCOUNTON;
@@ -56,37 +61,44 @@ SET @CMD_Template = N'WITH Exclusions AS
56
61
('
57
62
-- exclude modules invalid for schemabinding
58
63
+ N'
59
-
SELECT d.referenced_major_id AS object_id
64
+
SELECT d.object_id
60
65
from sys.sql_dependencies AS d
61
-
WHERE OBJECTPROPERTY(d.referenced_major_id, ''IsScalarFunction'') = 1 AND (
66
+
WHERE OBJECTPROPERTY(d.object_id, ''IsScalarFunction'') = 1 AND (
62
67
d.referenced_major_id = d.object_id
63
-
OR OBJECT_DEFINITION(d.referenced_major_id) LIKE N''%sp_OACreate%sp_OA%''
64
-
OR LOWER(OBJECT_DEFINITION(d.referenced_major_id)) LIKE N''%exec%sp_executesql%''
65
-
OR OBJECT_SCHEMA_NAME(d.referenced_major_id) IN (''tSQLt'',''sys'')
68
+
OR OBJECT_DEFINITION(d.object_id) LIKE N''%sp_OACreate%sp_OA%''
69
+
OR LOWER(OBJECT_DEFINITION(d.object_id)) LIKE N''%exec%sp_executesql%''
70
+
OR LOWER(OBJECT_DEFINITION(d.object_id)) LIKE N''%OPENROWSET%''
71
+
OR LOWER(OBJECT_DEFINITION(d.object_id)) LIKE N''%OPENQUERY%''
72
+
OR OBJECT_SCHEMA_NAME(d.object_id) IN (''tSQLt'',''sys'')
66
73
)
67
74
UNION ALL
68
-
select d.referenced_id
75
+
select d.referencing_id
69
76
from sys.sql_expression_dependencies AS d
70
-
WHERE OBJECTPROPERTY(d.referenced_id, ''IsScalarFunction'') = 1 AND (
77
+
WHERE OBJECTPROPERTY(d.referencing_id, ''IsScalarFunction'') = 1 AND (
71
78
d.referencing_id = d.referenced_id
72
-
OR OBJECT_DEFINITION(d.referenced_id) LIKE N''%sp_OACreate%sp_OA%''
73
-
OR LOWER(OBJECT_DEFINITION(d.referenced_id)) LIKE N''%exec%sp_executesql%''
74
-
OR OBJECT_SCHEMA_NAME(d.referenced_id) IN (''tSQLt'',''sys'')
75
-
)'
79
+
OR OBJECT_DEFINITION(d.referencing_id) LIKE N''%sp_OACreate%sp_OA%''
80
+
OR LOWER(OBJECT_DEFINITION(d.referencing_id)) LIKE N''%exec%sp_executesql%''
81
+
OR LOWER(OBJECT_DEFINITION(d.referencing_id)) LIKE N''%OPENROWSET%''
82
+
OR LOWER(OBJECT_DEFINITION(d.referencing_id)) LIKE N''%OPENQUERY%''
83
+
OR OBJECT_SCHEMA_NAME(d.referencing_id) IN (''tSQLt'',''sys'')'+
84
+
CASEWHEN @IgnoreFunctionsWithCrossDatabaseDependencies =1THENCONVERT(nvarchar(max), N'
85
+
OR d.referenced_database_name <> DB_NAME()')
86
+
ELSEN''
87
+
END+N')'
76
88
-- dependant on synonyms
77
-
+CASEWHEN @IgnoreFunctionsDependentOnSynonyms =1THEN N'
89
+
+CASEWHEN @IgnoreFunctionsDependentOnSynonyms =1THENCONVERT(nvarchar(max), N'
78
90
UNION ALL
79
91
select d.object_id
80
92
from sys.sql_dependencies AS d
81
93
INNER JOIN sys.synonyms AS syn ON d.referenced_major_id = syn.object_id
82
94
UNION ALL
83
95
select d.referencing_id
84
96
from sys.sql_expression_dependencies AS d
85
-
INNER JOIN sys.synonyms AS syn ON d.referenced_id = syn.object_id'
97
+
INNER JOIN sys.synonyms AS syn ON d.referenced_id = syn.object_id')
86
98
ELSEN''
87
99
END
88
100
-- has constraint dependencies
89
-
+CASEWHEN @IgnoreFunctionsWithConstraintDependencies =1THEN N'
101
+
+CASEWHEN @IgnoreFunctionsWithConstraintDependencies =1THENCONVERT(nvarchar(max), N'
90
102
UNION ALL
91
103
select d.referenced_major_id
92
104
from sys.sql_dependencies AS d
@@ -96,11 +108,21 @@ UNION ALL
96
108
select d.referenced_id
97
109
from sys.sql_expression_dependencies AS d
98
110
INNER JOIN sys.sysconstraints AS con ON d.referencing_id = con.constid
99
-
WHERE OBJECTPROPERTY(d.referenced_id, ''IsScalarFunction'') = 1'
111
+
WHERE OBJECTPROPERTY(d.referenced_id, ''IsScalarFunction'') = 1')
112
+
ELSEN''
113
+
END
114
+
-- Has table-valued parameters
115
+
+CASEWHEN @IgnoreFunctionsWithTableValuedParameters =1THENCONVERT(nvarchar(max), N'
116
+
UNION ALL
117
+
SELECT p.object_id
118
+
FROM sys.parameters AS p
119
+
INNER JOIN sys.types AS t ON p.user_type_id = t.user_type_id AND p.system_type_id = t.system_type_id
120
+
WHERE t.is_table_type = 1
121
+
')
100
122
ELSEN''
101
123
END
102
124
-- Recursive depdendencies
103
-
+ N'
125
+
+CONVERT(nvarchar(max), N'
104
126
), ExclusionTree1 AS
105
127
(
106
128
select d.object_id
@@ -114,10 +136,9 @@ from sys.sql_dependencies AS d
114
136
INNER JOIN ExclusionTree1 AS Tree ON d.referenced_major_id = Tree.object_id
115
137
WHERE OBJECTPROPERTY(d.referenced_major_id, ''IsScalarFunction'') = 1
116
138
AND d.referenced_major_id <> d.object_id
117
-
)'
139
+
)')
118
140
-- Recursive expression-based dependencies
119
-
/*
120
-
+ N'
141
+
+CONVERT(nvarchar(max), N'
121
142
, ExclusionTree2 AS
122
143
(
123
144
select d.referencing_id AS object_id
@@ -129,8 +150,8 @@ select d.referencing_id
129
150
from sys.sql_expression_dependencies AS d
130
151
INNER JOIN ExclusionTree2 AS Tree ON d.referenced_id = Tree.object_id
0 commit comments