snippetsqlCritical
How do I list or search all the column names in my database?
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?
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.
You can make use of information_schema views to list all objects in SQL Server 2005 or 2008 databases.
http://blog.sqlauthority.com/2008/08/06/sql-server-query-to-find-column-from-all-tables-of-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.columnshttp://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.columnsContext
StackExchange Database Administrators Q#511, answer score: 51
Revisions (0)
No revisions yet.