-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsp_dependencies.sql
More file actions
62 lines (58 loc) · 3.07 KB
/
sp_dependencies.sql
File metadata and controls
62 lines (58 loc) · 3.07 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
ALTER PROCEDURE [dbo].[sp_dependencies] @ObjectName sysname,
@Depth int = NULL,
@FilterTypes bit = 1
AS
SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
DECLARE @ObjectID int = OBJECT_ID(@ObjectName)
;WITH DependenciesCTE
(
fromName,
fromID,
fromDesc,
toName,
toID,
toDesc,
[Level]
)
AS
(
SELECT @ObjectName referencing_name,
referencing_id,
O1.type_desc,
referenced_entity_name,
OBJECT_ID(referenced_entity_name) referenced_id,
O2.type_desc,
0
FROM sys.sql_expression_dependencies D
LEFT JOIN sys.objects O1 ON O1.object_id = D.referencing_id
LEFT JOIN sys.objects O2 ON O2.object_id = OBJECT_ID(D.referenced_entity_name)
WHERE D.referencing_id = @ObjectID
AND D.referenced_class = 1
AND D.referencing_minor_id = 0
AND (O2.type IN ('P', 'FN', 'IF') OR @FilterTypes = 0)
UNION ALL
SELECT C.toName,
C.toID,
O1.type_desc,
D.referenced_entity_name,
OBJECT_ID(D.referenced_entity_name) referenced_id,
O2.type_desc,
C.Level + 1
FROM sys.sql_expression_dependencies D
INNER JOIN DependenciesCTE C ON D.referencing_id = C.toID
INNER JOIN sys.objects O1 ON O1.object_id = D.referencing_id
INNER JOIN sys.objects O2 ON O2.object_id = OBJECT_ID(D.referenced_entity_name)
WHERE C.Level < ISNULL(@Depth, C.Level+1)
AND D.referenced_class = 1
AND D.referencing_minor_id = 0
AND (O2.type IN ('P', 'FN', 'IF') OR @FilterTypes = 0)
)
SELECT fromName,
fromID,
fromDesc,
toName,
toID,
toDesc,
[Level]
FROM DependenciesCTE