-
Notifications
You must be signed in to change notification settings - Fork 1
/
DropSchemaBinding.sql
143 lines (125 loc) · 6.66 KB
/
DropSchemaBinding.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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/************************************************************************************************************************************
xxxxx SCHEMABINDING removal script
If the deployment goes past xxxxx then the schemabinding has been added to all of the functions
This will cause the next run of the deployment to fail so this script removes it all so that the
deployment can be re-run
************************************************************************************************************************************/
SELECT
OBJECT_SCHEMA_NAME(id) + '.' + OBJECT_NAME(id)
FROM
sysobjects
WHERE
OBJECT_DEFINITION(id) LIKE '%WITH SCHEMABINDING%';
IF object_id('tempdb..#idList') IS NOT NULL
DROP TABLE #idList;
DECLARE @PositionShemaBinding INT;
DECLARE @Command NVARCHAR(MAX);
DECLARE @ObjectName VARCHAR(MAX);
DECLARE @ObjectType VARCHAR(MAX);
DECLARE @UserMessage VARCHAR(MAX);
WITH CTE_deplist AS
(
SELECT
1 AS DepLevel,
o.id AS ObjectID,
CASE o.xtype
WHEN 'V' THEN 'VIEW'
WHEN 'P' THEN 'PROCEDURE'
WHEN 'FN' THEN 'FUNCTION'
WHEN 'IF' THEN 'FUNCTION'
WHEN 'TF' THEN 'FUNCTION'
ELSE NULL
END AS ObjectType
FROM
sysobjects o
LEFT JOIN
sysdepends d
ON
d.depid = o.id
WHERE
d.depid IS NULL
AND
OBJECT_DEFINITION(o.id) LIKE '%WITH SCHEMABINDING%'
UNION ALL
SELECT
DepLevel + 1 AS DepLevel,
o.id AS ObjectID,
CASE o.xtype
WHEN 'V' THEN 'VIEW'
WHEN 'P' THEN 'PROCEDURE'
WHEN 'FN' THEN 'FUNCTION'
WHEN 'IF' THEN 'FUNCTION'
WHEN 'TF' THEN 'FUNCTION'
ELSE NULL
END AS ObjectType
FROM
CTE_deplist dl
INNER JOIN
sysdepends d
ON
d.id = dl.ObjectID
INNER JOIN
sysobjects o
ON
d.depid = o.id
WHERE
OBJECT_DEFINITION(o.id) LIKE '%WITH SCHEMABINDING%'
AND
d.depnumber = 0
), CTE_ListFinal AS
( SELECT
MAX(DepLevel) AS DepLevel,
ObjectID,
ObjectType
FROM
CTE_deplist
WHERE
ObjectType IS NOT NULL
GROUP BY
ObjectID,
ObjectType
)
SELECT
ROW_NUMBER() OVER (ORDER BY DepLevel, ObjectID, ObjectType) AS RowNumber,
DepLevel,
ObjectID,
ObjectType
INTO
#idList
FROM
CTE_ListFinal
ORDER BY
DepLevel,
ObjectID;
DECLARE csrIDList cursor LOCAL FORWARD_ONLY READ_ONLY for
SELECT
OBJECT_DEFINITION(ObjectID),
ObjectType,
'[' + object_schema_name(ObjectID) + '].[' + object_name(ObjectID) + ']' AS ObjectName
FROM
#idList
ORDER BY
RowNumber;
OPEN csrIDList;
FETCH NEXT FROM csrIDList
INTO @Command, @ObjectType, @ObjectName;
WHILE @@FETCH_STATUS = 0
BEGIN
SET @UserMessage = format(getdate(), 'yyyy-MM-dd HH:mm:ss.fff : ') + 'Changing ' + @ObjectName
RAISERROR(@UserMessage, 0, 0) WITH NOWAIT
-- WITH SCHEMA BINDING IS NOT PRESENT... Let's add it !
SET @Command = REPLACE(@Command, 'CREATE ' + @ObjectType, 'ALTER ' + @ObjectType);
SET @Command = REPLACE(@Command, 'WITH SCHEMABINDING', '');
--RAISERROR(@Command, 0, 0) WITH NOWAIT;
EXECUTE sp_executesql @Command;
FETCH NEXT FROM csrIDList
INTO @Command, @ObjectType, @ObjectName;
END
CLOSE csrIDList;
DEALLOCATE csrIDList;
SELECT
OBJECT_SCHEMA_NAME(id) + '.' + OBJECT_NAME(id)
FROM
sysobjects
WHERE
OBJECT_DEFINITION(id) LIKE '%WITH SCHEMABINDING%';