A few years ago, I needed to find a query that I had written in a SQL Stored Procedure but after clicking through several of them and striking out, I decided that there had to be a better way. I ended up writing a simple Stored Procedure with a single search parameter that would return a stored procedure definition and excerpt from it. I like it so much that I’ve added it to every database I’ve managed since then. It was a quick and dirty solution to a problem and I never really put much more thought into it - until tonight.
I was on the Windows Admins Slack tonight working on some SQL queries in the SCCM/ConfigMgr database with Riley Childs and Cody Mathis and during the course of the conversation, Cody shared a short query to find columns with a specific name in the database which got me thinking about my Stored Procedure. I took a look at what I was using and realized that we were in the same book, just on different pages. I took a few minutes and combined our logic and added a few extra bits to make a nicer version of the query. It will now return Table, View, Stored Procedure, and Function names where the object name, column name or object definition match your search criteria. It’s simple but effective. Hope you find it useful as well.
I’ve added it to my GitHub repo, which I will update as needed, or you can grab the code below:
1--Change the USING to the DB you want to run this against.USE [CM_ASD] 2 3--Change the value to your search term 4DECLARE @Text nvarchar(1000) 5SET @Text = 'TextToFind' 6 7 8----Main Query-- 9 10DECLARE @newText varchar(1000) 11SET @NewText = '%' + @Text + '%' 12 13SELECT 14 DISTINCT 15 'TableOrView' AS 'ObjectType' 16 ,TABLE_NAME AS 'ObjectName' 17 ,CASE WHEN COLUMN_NAME LIKE @NewText THEN COLUMN_NAME ELSE NULL END AS 'ColumnName' 18 ,NULL AS 'Excerpt' 19 ,NULL AS 'Definition' 20FROM 21 INFORMATION_SCHEMA.COLUMNS 22WHERE 23 TABLE_NAME LIKE @NewText OR 24 COLUMN_NAME LIKE @NewText 25 26UNION 27 28SELECT 29 DISTINCT 30 'StoredProcedure' AS 'ObjectType' 31 ,SPECIFIC_NAME AS 'ObjectName' 32 ,NULL AS 'ColumnName' 33 ,SUBSTRING(ROUTINE_DEFINITION, CHARINDEX(@Text, ROUTINE_DEFINITION)-50, 100) AS 'Excerpt' 34 ,ROUTINE_DEFINITION AS 'Definition' 35FROM 36 INFORMATION_SCHEMA.ROUTINES 37WHERE 38 SPECIFIC_NAME LIKE @NewText OR 39 ROUTINE_DEFINITION LIKE @NewText 40 41UNION 42 43SELECT 44 DISTINCT 45 'ViewDefinition' AS 'ObjectType' 46 ,TABLE_NAME AS 'ObjectName' 47 ,NULL AS 'ColumnName' 48 ,SUBSTRING(VIEW_DEFINITION, CHARINDEX(@text, VIEW_DEFINITION)-50, 100) as 'Excerpt' 49 ,VIEW_DEFINITION AS 'Definition' 50FROM 51 INFORMATION_SCHEMA.VIEWS 52WHERE 53 TABLE_NAME LIKE @NewText OR 54 VIEW_DEFINITION LIKE @newText 55 56UNION 57 58SELECT 59 DISTINCT 60 'Function' AS 'ObjectType' 61 ,TABLE_NAME AS 'ObjectName' 62 ,COLUMN_NAME AS 'ColumnName' 63 ,NULL AS 'Excerpt' 64 ,NULL AS 'Definition' 65FROM 66 INFORMATION_SCHEMA.ROUTINE_COLUMNS 67WHERE 68 COLUMN_NAME LIKE @newText 69ORDER BY 70 ObjectType, 71 ColumnName, 72 ObjectName 73
Also, if you’re looking for something more robust that can search within the database data itself (plus much more), check out ApexSQL Search. I just started using it a few weeks ago and it really is fantastic. Best of all, it’s FREE, along with several other tools that I plan to try at some point.