Skip to content

Instantly share code, notes, and snippets.

@theShiva
Created October 2, 2012 17:44
Show Gist options
  • Save theShiva/3821574 to your computer and use it in GitHub Desktop.
Save theShiva/3821574 to your computer and use it in GitHub Desktop.
Search All Column Names In Sql Server Tables For A String / Pattern
use Northwind
go
-- SQL run against Northwind sample database to find all tables
-- that have CustomerID in their column / field names.
-- replace CustomerID in between % % with whatever it is you are searching for
select distinct tab.name + '.' + col.name as 'Table.ColumnName'
from sys.tables tab
join sys.columns col on (tab.object_id = col.object_id)
join sys.types types on (col.system_type_id = types.system_type_id)
where tab.type_desc ='USER_TABLE'
and col.name like '%CustomerID%'
and types.name IN ('CHAR', 'NCHAR', 'VARCHAR', 'NVARCHAR')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment