Click here to Skip to main content
15,917,709 members
Articles / Database Development / SQL Server / SQL Server 2008

SQL Server Easy Column Search

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
11 Jan 2010CPOL2 min read 31.2K   11   14   5
Column search functionality that works as easy as sp_help.

Introduction

When developing queries in SQL Server, you might have come across the need to list all the tables that have a specific column name. Sure, you can write a query using syscolumns, sysobjects etc., but you will have to write it every time or save it somewhere - I am too lazy for that. In this article, we'll create a Stored Procedure that will work as easy as sp_help, only for columns!

Background

To give a better idea, below is the query that we would normally use to search for a column name:

SQL
SELECT sysobjects.name as "Table", 
syscolumns.name as "Column"
from sysobjects  , syscolumns 
where sysobjects.id = syscolumns.id
and sysobjects.xtype = 'u'
and syscolumns.name like '%order%'
order by sysobjects.name, syscolumns.name

This will return all tables where the column name contains the text 'order'. As the results are what we're looking for, we'll use this query as the basis for our solution.

Using the code

As I have mentioned earlier, we are looking for a solution similar to sp_help; therefore, we'll create a Stored Procedure on the Master database of the SQL Server instance.

The code and principle are simple, but works really cool. First, we need the database of the active query window:

SQL
SET @DataBase = (SELECT DB_NAME())

Now, we build some dynamic SQL with the database name of our active query window (same query as above) and execute it.

SQL
SET @SQL = 'SELECT a.name AS [Table],'
SET @SQL = @SQL + ' b.name as [Column]'
SET @SQL = @SQL + ' from ' + @DataBase + '..sysobjects a , ' 
SET @SQL = @SQL +  @DataBase + '..syscolumns b' 
SET @SQL = @SQL + ' where a.id = b.id'
SET @SQL = @SQL + ' and a.xtype = ''u'''
SET @SQL = @SQL + ' and b.name like ''' + '%' + @ColumnName + '%''' + ''
SET @SQL = @SQL + ' order by a.name, b.name'

EXEC(@SQL)

Voila, once we have created the Stored Procedure, we will be able to search for column names from the active query window in the following way:

SQL
sp_column order

which will give us the following results when using the Northwind database:

Image 1

But wait, there's more

We can configure a keyboard shortcut for this in SQL Server Management Studio:

From the Tools menu, select Options. From the left hand pane under Environment, select Keyboard. In one of the available Query shortcuts, enter sp_column:

Image 2

Now we can easily highlight the name of the column we're searching for, e.g., order and hit Ctrl + 3.

Points of interest

While I was doing this, I also came across syscomments, which enables us to search for phrases in Stored Procedures and functions. That's a short and sweet principle, and I will post that to the Tips and Tricks section of The Code Project. Enjoy!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
South Africa South Africa
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralGood Pin
db_developer8-Jan-10 5:46
db_developer8-Jan-10 5:46 
GeneralRe: Good Pin
Divan van der Watt11-Jan-10 1:17
Divan van der Watt11-Jan-10 1:17 
GeneralRe: Good Pin
db_developer14-Jan-10 1:26
db_developer14-Jan-10 1:26 
GeneralUse INFORMATION_SCHEMA.COLUMNS instead Pin
Adam Dawes6-Jan-10 4:11
Adam Dawes6-Jan-10 4:11 
GeneralRe: Use INFORMATION_SCHEMA.COLUMNS instead Pin
spoodygoon6-Jan-10 7:29
spoodygoon6-Jan-10 7:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.