SQL Search Text Of Views, Stored Procs And Tables
Updated: Mar 21
Searching through databases can be a tedious and time-consuming task, especially when trying to locate text in views or stored procedures. When searching for this type of text, it is important to use the correct T-SQL commands. With today’s blog post we’ll delve into the how T-SQL searches through database schema objects (views and procs) to find specific text and patterns that are present in those objects. We’ll explore why using T-SQL is beneficial for locating these details within different types of object as well as how individual pieces work together when searching throughout a database structure successfully. Join us as we dive deeper into understanding the best ways to tackle search queries with T-SQL.
Here are several ways to search for text in SQL Server:
Using Object Explorer
Using Object Explorer in SQL Server Management Studio (SSMS): In SSMS, expand the database that you want to search in, right-click on it, and select "Object Explorer Details". In the Object Explorer Details pane, select the object type (such as "Stored Procedures", "Views", or "Tables") and search for the desired text in the "Definition" column.
After scritping out tables, views and procs you can use the "Find and Replace" feature in SSMS: In SSMS, open the query editor and press "Ctrl+Shift+F" to open the "Find and Replace" dialog box. Select the desired search options (such as "Entire Solution" or "Current Project"), enter the search term, and choose the object type to search in (such as "Stored Procedures" or "Views").
Using the system tables:
Using the system tables: You can also search for text in the system tables directly. For example, you can use the following query to search for a specific text in all stored procedures in a database:
SELECT name FROM sys.procedures WHERE OBJECT_DEFINITION(object_id) LIKE '%your_text_here%'
More complex example:
Here's an example T-SQL code that will loop through every user database (excluding system databases) and search for a specified word in the definition of stored procedures and views:
DECLARE @SearchWord NVARCHAR(100) = 'specified_word'; DECLARE @DatabaseName NVARCHAR(100); DECLARE @SQL NVARCHAR(MAX); DECLARE db_cursor CURSOR FOR SELECT name FROM sys.databases WHERE name NOT IN ('master', 'model', 'msdb', 'tempdb') ORDER BY name; OPEN db_cursor; FETCH NEXT FROM db_cursor INTO @DatabaseName; WHILE @@FETCH_STATUS = 0 BEGIN DECLARE @SQL NVARCHAR(MAX); Declare @X as Varchar(25) Set @X = 'subscription type' Set @SQL = '' SET @SQL = @SQL + N'USE ' + QUOTENAME(@DatabaseName) + ';'+ 'DECLARE @SearchWord NVARCHAR(100) Set @SearchWord = '+CHAR(39)+@X+CHAR(39)+' '+' SELECT QUOTENAME(s.name) AS schema_name, SELECT QUOTENAME(s.name) AS schema_name, QUOTENAME(o.name) AS object_name, CASE o.type WHEN ''P'' THEN ''Stored procedure'' WHEN ''V'' THEN ''View'' END AS object_type FROM sys.objects o INNER JOIN sys.schemas s ON o.schema_id = s.schema_id WHERE (o.type = ''P'' OR o.type = ''V'') AND OBJECT_DEFINITION(OBJECT_ID(QUOTENAME(s.name) + ''.'' + QUOTENAME(o.name))) LIKE ''%'' + @SearchWord + ''%'' ORDER BY schema_name, object_name'; EXEC sp_executesql @SQL; FETCH NEXT FROM db_cursor INTO @DatabaseName; END CLOSE db_cursor; DEALLOCATE db_cursor;
This code uses a cursor to loop through every user database (excluding the system databases) and dynamically generates a SQL statement to search for the specified word in the definition of stored procedures and views. The QUOTENAME function is used to handle any special characters in the object names or database names. The search is case-insensitive and searches for the specified word anywhere in the object definition. The result includes the schema name, object name, and object type.
Using PowerShell: You can use PowerShell to search for text in SQL Server objects. For example, you can use the following script to search for a specific text in all stored procedures in a database:
$server = "your_server_name" $database = "your_database_name" $searchText = "your_text_here" Invoke-Sqlcmd -ServerInstance $server -Database $database -Query " SELECT name FROM sys.procedures WHERE OBJECT_DEFINITION(object_id) LIKE '%$searchText%'"
You can modify this script to search for text in other types of objects as well.
Using third-party tools
Using third-party tools: There are also third-party tools available that can search for text in SQL Server objects, such as ApexSQL Search or Redgate SQL Search. These tools typically offer more advanced search options and can search across multiple databases or even entire SQL Server instances.
Redgate SQL Search is a free tool for Microsoft SQL Server that allows users to search for database objects and data across multiple databases. Here are some of its features:
Object search: SQL Search enables users to search for database objects (tables, stored procedures, views, functions, etc.) by name, keyword, or wildcard. It also allows users to filter their search by database or object type.
Text search: In addition to object search, SQL Search allows users to search for text within database objects. This includes DDL scripts, stored procedures, functions, views, triggers, and more.
Wildcard search: SQL Search supports wildcard searches in both object and text search. This makes it easy to find objects or text that match a specific pattern or naming convention.
Object preview: When users find an object in their search results, SQL Search provides a preview of the object's definition or script. This allows users to quickly see the details of the object without having to navigate to it in SQL Server Management Studio.
Cross-database search: SQL Search enables users to search for objects and text across multiple databases. This is particularly useful for DBAs who need to search for specific objects or text across all of their databases.
Integration with SSMS: SQL Search is integrated with SQL Server Management Studio (SSMS) and can be accessed directly from the SSMS menu. This makes it easy to launch a search without having to switch between tools.
Overall, Redgate SQL Search is a powerful and easy-to-use tool for searching SQL Server databases. Its features make it a valuable tool for DBAs, developers, and other SQL Server users who need to quickly find and analyze database objects and data.