-
Notifications
You must be signed in to change notification settings - Fork 681
/
dbo.usp_SearchStoredProcedureText.sql
91 lines (77 loc) · 2.82 KB
/
dbo.usp_SearchStoredProcedureText.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
SET NOEXEC OFF;
IF OBJECT_ID('dbo.sp_foreachdb', 'P') IS NULL
RAISERROR('Please install before awesome stored procedure https://github.com/ktaranov/SQL-Server-First-Responder-Kit/blob/master/sp_foreachdb.sql', 16, 1);
SET NOEXEC ON;
GO
IF OBJECT_ID('dbo.usp_SearchStoredProcedureText', 'P') IS NULL
EXECUTE ('CREATE PROCEDURE dbo.usp_SearchStoredProcedureText AS RETURN');
GO
ALTER PROCEDURE dbo.usp_SearchStoredProcedureText(
@searchforthis NVARCHAR(512)
, @object_type VARCHAR(2) = NULL
, @is_ms_shipped BIT = 0
)
AS
/*
.DESCRIPTION
Search text in stored procedures
.EXAMPLE
EXEC dbo.usp_SearchStoredProcedureText @searchforthis = 'sp_foreachdb';
.NOTE
Author: Tim Ford
Original link: https://www.itprotoday.com/microsoft-sql-server/using-sqlmodules-system-catalog-view-search-function-and-stored-procedure
*/
SET NOCOUNT ON;
DECLARE @search_text NVARCHAR(MAX);
CREATE TABLE #search_results
(
the__database sysname NOT NULL,
the__schema sysname NOT NULL,
object__name sysname NOT NULL,
object__type NVARCHAR(120) NOT NULL,
is_ms_shipped BIT NOT NULL,
sql__text NVARCHAR(MAX) NOT NULL
);
IF @object_type IS NULL
BEGIN
SELECT @search_text =
'USE ?;
INSERT INTO #search_results (the__database, the__schema, object__name, object__type, is_ms_shipped, sql__text)
SELECT db_name() AS the__database
, OBJECT_SCHEMA_NAME(O.object_id) AS the__schema
, O.name AS object__name
, O.type_desc AS object__type
, O.is_ms_shipped
, M.definition AS sql__text
FROM sys.objects O WITH(NOLOCK)
LEFT JOIN sys.sql_modules M ON O.object_id = M.object_id
WHERE O.is_ms_shipped = ' + CAST(@is_ms_shipped AS VARCHAR(1)) + ' AND M.definition LIKE ''%' + @searchforthis + '%''' + ';';
END;
ELSE
BEGIN
SELECT @search_text =
'USE ?;
INSERT INTO #search_results (the__database, the__schema, object__name, object__type, is_ms_shipped, sql__text)
SELECT db_name() AS the__database
, OBJECT_SCHEMA_NAME(O.object_id) AS the__schema
, O.name AS object__name
, O.type_desc AS object__type
, O.is_ms_shipped
, M.definition AS sql__text
FROM sys.objects O WITH(NOLOCK)
LEFT JOIN sys.sql_modules M ON O.object_id = M.object_id
WHERE O.is_ms_shipped = ' + CAST(@is_ms_shipped AS VARCHAR(1)) + ' AND O.type = ''' + @object_type + '''' +
'AND M.definition LIKE ''%' + @searchforthis + '%''' + ';';
END;
EXEC dbo.sp_foreachdb @command = @search_text;
SELECT the__database
, the__schema
, object__name
, object__type
, is_ms_shipped
, sql__text
FROM #search_results
ORDER BY the__database
, the__schema
, object__name;
GO