HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlCritical

How do I list or search all the column names in my database?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
thesearchallcolumnnamesdatabasehowlist

Problem

I want to search for a string in the names of the columns present in a database.

I’m working on a maintenance project and some of the databases I deal with have more than 150 tables, so I'm looking for a quick way to do this.

What do you recommend?

Solution

You can use following query to list all columns or search columns across tables in a database.

USE AdventureWorks 
GO 
SELECT t.name AS table_name,
SCHEMA_NAME(schema_id) AS schema_name,
c.name AS column_name
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
WHERE c.name LIKE '%EmployeeID%'
ORDER BY schema_name, table_name;


You can make use of information_schema views to list all objects in SQL Server 2005 or 2008 databases.

SELECT * FROM information_schema.tables

SELECT * FROM information_schema.columns


http://blog.sqlauthority.com/2008/08/06/sql-server-query-to-find-column-from-all-tables-of-database/

Code Snippets

USE AdventureWorks 
GO 
SELECT t.name AS table_name,
SCHEMA_NAME(schema_id) AS schema_name,
c.name AS column_name
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
WHERE c.name LIKE '%EmployeeID%'
ORDER BY schema_name, table_name;
SELECT * FROM information_schema.tables

SELECT * FROM information_schema.columns

Context

StackExchange Database Administrators Q#511, answer score: 51

Revisions (0)

No revisions yet.