You are here:   Home
Register   |  Login

Captured Technology - Blog

Minimize

Another Great SQL Server Grep Function

Sep 8

Written by:
9/8/2009 10:33 PM  RssIcon

Another Great SQL Server Grep Function

The first SQL Server Grep function I posted is one of the most popular items on the site to today therefore I thought I would share another great version of the MS SQL string search function you can use to search for terms within your database. This version uses a temporary table to store the results from a cursor and then displays the results to the output screen before dropping the temporary table.

create procedure searchforstring
@searchstring varchar(100)
as
begin
declare @SQLString as varchar(1000)
declare @TableName as varchar(255)
declare @ColumnName as varchar(255)
declare @Count as int

DECLARE C_SOURCE CURSOR FOR  
select sysobjects.name tablename,syscolumns.name columnname,'select top 1 * 
from ['
+ sysobjects.name + ']
where '
+ syscolumns.name
+' like ''%' + @searchstring + '%'' ' SQL_String from syscolumns , sysobjects, systypes where sysobjects.xtype='U' and sysobjects.id = syscolumns.id and
systypes.xtype=syscolumns.xtype and systypes.name <> 'sysname' and systypes.name in ('varchar','char','nvarchar','nchar','text','text') order by sysobjects.name,syscolumns.colid create table #result(outputstring varchar(500)) OPEN C_SOURCE FETCH NEXT FROM C_SOURCE INTO @TableName,@ColumnName,@SQLstring WHILE @@FETCH_STATUS = 0 begin exec(@SQLString) --print @SQLString if @@rowcount > 0 insert into #result values( '[' + @TableName + '].' + @ColumnName) FETCH NEXT FROM C_SOURCE INTO @TableName,@ColumnName,@SQLstring END CLOSE C_SOURCE DEALLOCATE C_SOURCE select * from #result drop table #result END

Tags:
Categories:

1 comment(s) so far...


Gravatar

Re: Another Great SQL Server Grep Function

SQL server grep is a command line text search utility written originally for Unix..The only probable limitation for this utility is that the destination database should be in *.txt format..along with that The best thing about the SQL Server Grep utility command is that it has been enhanced to be upwardly compatible with the historical functionality of egrep and fgrep commands too..

By Venice Hotels on   8/31/2010 4:51 AM

Your name:
Gravatar Preview
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Security Code
CAPTCHA image
Enter the code shown above in the box below
Add Comment   Cancel 

FaceBook

Minimize

Mobile Version

Minimize
Add CapturedTech - Technology Mippin widget

Translate

Minimize

Sponsors

Minimize
Promotional Banner- Free Shipping on New and Used Books- Red- 120 x 600

Recent Comments

Minimize
Re: Microsoft Test Bounties
I had spent a good deal of my time looking for someone to explain this subject clearly and I have really enjoyed reading your blog posts and I collected a lot of interesting things as well as I done a research on the subject and learn most peoples will agree with your blog.
Re: 10 Ways to Source Great Sales Leads
Hoovers.com, that have contact information for tens of millions of businesses. If necessary, you can work with these companies' research teams to order lists specifically tailored to your organization's sales needs.
Re: Microsoft Test Bounties
To find bugs and showing it to microsoft is quite admirable. Keep blogging more i will appreciate.
Re: Microsoft Test Bounties
Testing a software and giving out the errors to microsoft is amazing. Very good thing to do. One can do this in free time.
Re: Reindexing SQL Server Tables
Discussed about the SQL server and more interesting matter discussed in the post because in the post define about the SQL server table so that can be know that and able to understand regarding post.

u comment, i follow