48

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?

Nick Chammas
  • 14,810
  • 17
  • 76
  • 124
Webber
  • 695
  • 1
  • 6
  • 8

5 Answers5

52

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/

Sandeep Kumar M
  • 4,682
  • 3
  • 33
  • 35
9

There is also SQL Search - a free tool that integrates with SQL Server Management Studio.

alt text

Marek Grzenkowicz
  • 1,495
  • 2
  • 18
  • 27
5

Late one but hopefully useful since both tools are free.

ApexSQL Search – good thing about this tool is that it can also search data, show dependencies between objects and couple other useful things.

enter image description here

SSMS Toolpack – free for all versions except SQL 2012. A lot of great options that are not related only to searching such as snippets, various customizations and more.

enter image description here

Stanley Norman
  • 349
  • 3
  • 3
0

This is one that all SQL DBA from old time use.

EXEC sp_help 'your table name';

If only with single line, you don't even need to type EXEC. Just do

sp_help 'your table name'
Paul White
  • 94,921
  • 30
  • 437
  • 687
PhilRoan
  • 9
  • 1
0

Here is a way to search all the databases and tell you which database has a table.column:

DECLARE @command varchar(1000)
SET @command =
'USE ? IF EXISTS (
SELECT 1
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
where t.name = ''TableName'' 
and c.name = ''ColumnName''
) select ''?'''
EXEC sp_MSforeachdb @command
ttomsen
  • 289
  • 2
  • 6