SQL Search Objects Names
Posted February 15, 2023
By Kevin Schwickrath1 min read
Let’s say you have several databases on a Microsoft SQL Server, each with loads of tables. Now you want to find a table with the word ‘users’ in the table name. Easy, run a SQL query to search the table names.
Super simple!
Search SQL tables names with this query.
SELECT *FROM sys.tablesWHERE name LIKE '%users%'How about with ‘cheetah’.
SELECT *FROM sys.tablesWHERE name LIKE '%cheetah%'The percent symbol ’%’ on either side of the word is a wildcard. As in match anything_users_anything or anything_cheetah_anything.
You can take the same query and search other SQL server other objects.
With this query I search ‘sys.views’ for anything containing ‘users’ in the name.
SELECT *FROM sys.viewsWHERE name LIKE '%users%'This query searches ‘sys.procedures’ for anything containing ‘users’ in the name.
SELECT *FROM sys.proceduresWHERE name LIKE '%users%'With this query I’m searching for “code” in all the table and column names of my selected database.
SELECT c.name AS 'ColumnName', t.name AS 'TableName'FROM sys.columns cJOIN sys.tables t ON c.object_id = t.object_id
WHERE c.name LIKE '%users%'ORDER BY TableName, ColumnNameHappy databasing!
This post is licensed under CC BY 4.0 by the author.
Related Posts
Starting Percona XtraDB Cluster From Cold Boot or Crash
A Percona XtraDB Cluster can be a resilient option for high-uptime databases. What if you lose all your servers due to a power failure? Restarting a…
Exporting All SSRS Reports
I want to backup reports within SSRS with a bulk export.