Click here to Skip to main content
Licence CPOL
First Posted 6 Jan 2010
Views 8,100
Bookmarked 14 times

SQL Server Easy Column Search

By | 11 Jan 2010 | Article
Column search functionality that works as easy as sp_help.
 
Part of The SQL Zone sponsored by
See Also

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)

About the Author

Divan van der Watt

Software Developer

South Africa South Africa

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralGood PinmemberRoman_K5:46 8 Jan '10  
GeneralRe: Good PinmemberDivan van der Watt1:17 11 Jan '10  
GeneralRe: Good PinmemberRoman_K1:26 14 Jan '10  
GeneralUse INFORMATION_SCHEMA.COLUMNS instead PinmemberAdam Dawes4:11 6 Jan '10  
GeneralRe: Use INFORMATION_SCHEMA.COLUMNS instead Pinmemberspoodygoon7:29 6 Jan '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web03 | 2.5.120517.1 | Last Updated 11 Jan 2010
Article Copyright 2010 by Divan van der Watt
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid