-
Notifications
You must be signed in to change notification settings - Fork 0
/
DatabaseAdministration.fnDropCreateDefaultConstraintsWhereDefinitionContainsString
54 lines (49 loc) · 1.82 KB
/
DatabaseAdministration.fnDropCreateDefaultConstraintsWhereDefinitionContainsString
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
USE <DatabaseName>
GO
CREATE FUNCTION DatabaseAdministration.fnDropCreateDefaultConstraintsWhereDefinitionContainsString(
@SearchString nvarchar(1000)
)
/**************************************************************************************************
DATE AUTHOR
10/22/18 Alejandro
PURPOSE
Returns a set of SQL statemets that allow the user to drop and then re-create all default
constraints whose definition contains a specific string. Helpful for when you need to modify a
function that is referenced in numerous default constraints as SQL Server prevents this without
dropping and re-creating the constraints.
PARAMETERS
@SearchString String to search the constraints' definitions for
TICKETS
Date Ticket Description
10/19/18 NHITAP-41 Create credit card purchase expensing tool (created to help modify
function dbo.fnGetCurrentPayPeriodID)
TEST CODE
SELECT * FROM DatabaseAdministration.fnDropCreateDefaultConstraintsWhereDefinitionContainsString('fnGetCurrentPayPeriodID')
**************************************************************************************************/
RETURNS TABLE
AS
RETURN
(
SELECT
'ALTER TABLE ' + s.name + '.' + o.name + CHAR(13) + CHAR(10) +
'DROP CONSTRAINT ' + d.name + CHAR(13) + CHAR(10) AS DropConstraintSQL,
'ALTER TABLE ' + s.name + '.' + o.name + CHAR(13) + CHAR(10) +
' ADD CONSTRAINT ' + d.name + ' DEFAULT (' + CHAR(13) + CHAR(10) +
' ' + d.definition + CHAR(13) + CHAR(10) +
') FOR ' + c.name + CHAR(13) + CHAR(10) AS CreateConstraintSQL
FROM
sys.default_constraints AS d
INNER JOIN
sys.objects AS o ON
d.parent_object_id = o.object_id
INNER JOIN
sys.schemas AS s ON
o.schema_id = s.schema_id
INNER JOIN
sys.columns AS c ON
d.parent_object_id = c.object_id AND
d.parent_column_id = c.column_id
WHERE
definition LIKE '%' + @SearchString + '%'
);
GO