You want to find a column in one of the tables in your SQL Server database. Your database has a few hundred tables. Expanding all the tables to look for the column can be a time-consuming job. With these useful methods you find the column fast.
SQL Server has no search function to find a column in databases quickly. That can be useful when you look for a certain column but do not know which table it is in. The solution is a fairly simple query.
Method 1: SQL query on the metadata
SELECT O.NAME Tabelnaam,C.NAME KolomnaamFROM sys.columns CINNER JOIN sys.objects O ON C.object_id = O.object_idWHERE C.NAME LIKE '%DebiteurNaam%'ORDER BY O.NAME,C.NAMEOf course it is not very practical to type this whole query every time you look for a column. That is why there is also this stored procedure, which makes the job very easy:
CREATE PROCEDURE [dbo].[pcdGetColumns]@kolomnaam nvarchar(256)-- Zoek een kolomnaam in een database-- EXECUTE pcdGetColumns '%naam%'ASBEGIN
SELECT O.NAME Tabelnaam,C.NAME KolomnaamFROM sys.columns CINNER JOIN sys.objects O ON C.object_id = O.object_idWHERE C.NAME LIKE @kolomnaamORDER BY O.NAME, C.NAMEENDMethod 2: Red-Gate SQL Search
This is a really useful little tool from the vendor Red-Gate. I am a big fan of the tools Red-Gate sells anyway. Sadly they are not free and quite pricey, especially if the company you work for or do a project for does not have the tools. Luckily this tool is completely free. It is called SQL Search 2.
You install SQL Search 2 as an add-on in SQL Server Management Studio. You then get an extra button at the top that lets you search all databases, and also within different objects. You can choose to search only in tables, but you can also search in views, functions, procedures and triggers. Very cool.
I hope this helps you find a column in your database. If you have no rights to install software, choose the SQL query. If you can install software, choose the free SQL Search from Red-Gate.
Frequently asked questions
How do I find a column name in all tables of a SQL Server database?
Join sys.columns to sys.objects on object_id and filter on C.NAME LIKE '%DebiteurNaam%'. That shows the table name and column name of every column that matches.
Does SQL Server have a search function for columns?
No. SQL Server has no search function to find a column quickly. A simple query on the metadata solves this.
How do I create a stored procedure to find columns?
Create a procedure with the parameter @kolomnaam nvarchar(256) and the same query on sys.columns and sys.objects. Call it with EXECUTE pcdGetColumns '%naam%'.
What is Red-Gate SQL Search 2?
It is a free tool from Red-Gate that you install as an add-on in SQL Server Management Studio. It searches all databases, in tables, views, functions, procedures and triggers.
When do I choose the query and when SQL Search 2?
If you have no rights to install software, choose the SQL query. If you are allowed to install software, choose the free SQL Search 2 from Red-Gate.

Comments
Related blogs
Convert a date to an integer
Recursive Query with CTE I
Finding duplicate rows