Home
Manage Your Code
Snippet: SQL Search Tables and Fields (SQL)
Title: SQL Search Tables and Fields Language: SQL
Description: Search tables and fields containing some name Views: 965
Author: Joel Meikle Date Added: 11/27/2008
Copy Code  
-- Search all tables in current db scope with 'Customer' in the name
SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME LIKE '%Customer%'
GO

-- Search all columns in the current db scope with 'Customer' in the name
SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%Customer%'
GO
Usage
-- run in sql server management studio

-- use to create drop table commands (but not execute)
SELECT 'DROP TABLE ' + TABLE_NAME 
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME LIKE '%Customer%'
GO
-- copy the result set into query window & run to execute
Notes
SQL server 2005 and up (may with 2000 but not tested, not sure if 2000 has the INFORMATION_SCHEMA namespace)