-
Notifications
You must be signed in to change notification settings - Fork 44
/
fk untrusted or disabled check.sql
48 lines (41 loc) · 1.48 KB
/
fk untrusted or disabled check.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
--Check for untrusted or disabled FK's
--Could be a silent performance drag if FK's exist but aren't trusted.
--See also: "lab - fk untrusted or disabled check.ipnynb" or "lab - fk untrusted or disabled check.sql"
--Careful! Trusting or enabling a FK could cause application errors if invalid child table entries are expected
SELECT
Table_Name = s.name + '.' +o.name
, FK_Name = fk.name
, fk.is_not_trusted
, fk.is_disabled
, 'ALTER TABLE [' + s.name + '].[' +o.name +']
WITH CHECK
CHECK CONSTRAINT ['+fk.name+'];' --trusts and enables the FK
FROM sys.foreign_keys as fk
INNER JOIN sys.objects as o ON fk.parent_object_id = o.object_id
INNER JOIN sys.schemas as s ON o.schema_id = s.schema_id
where fk.is_not_trusted = 1
or fk.is_disabled = 1
/*
--Check all databases:
--Some not trusted FK's are common in the SSRS ReportServer and ReportServerTempDB databases
exec sp_msforeachdb 'use [?];
SELECT
DB_Name = ''?''
, Table_Name = s.name + ''.'' +o.name
, FK_Name = fk.name
, fk.is_not_trusted
, fk.is_disabled
, ''ALTER TABLE ['' + s.name + ''].['' +o.name +'']
WITH CHECK
CHECK CONSTRAINT [''+fk.name+''];'' --trusts and enables the FK
FROM sys.foreign_keys as fk
INNER JOIN sys.objects as o ON fk.parent_object_id = o.object_id
INNER JOIN sys.schemas as s ON o.schema_id = s.schema_id
where fk.is_not_trusted = 1
or fk.is_disabled = 1 ';
*/
/* --Sample:
ALTER TABLE [dbo].[table2]
WITH CHECK
CHECK CONSTRAINT [FK_table2_table1];
*/