Click here to Skip to main content
14,027,342 members
Click here to Skip to main content
Add your own
alternative version

Stats

24.9K views
11 downloads
14 bookmarked
Posted 6 Jan 2010
Licenced CPOL

SQL Server Easy Column Search

, 11 Jan 2010
Rate this:
Please Sign up or sign in to vote.
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:

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:

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.

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:

sp_column order

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

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:

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)

Share

About the Author

Divan van der Watt
Software Developer
South Africa South Africa
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralGood Pin
db_developer8-Jan-10 5:46
memberdb_developer8-Jan-10 5:46 
GeneralRe: Good Pin
Divan van der Watt11-Jan-10 1:17
memberDivan van der Watt11-Jan-10 1:17 
GeneralRe: Good Pin
db_developer14-Jan-10 1:26
memberdb_developer14-Jan-10 1:26 
GeneralUse INFORMATION_SCHEMA.COLUMNS instead Pin
Adam Dawes6-Jan-10 4:11
memberAdam Dawes6-Jan-10 4:11 
GeneralRe: Use INFORMATION_SCHEMA.COLUMNS instead Pin
spoodygoon6-Jan-10 7:29
memberspoodygoon6-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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190419.4 | Last Updated 11 Jan 2010
Article Copyright 2010 by Divan van der Watt
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid