Searching keywords in SQL Server Database

June 23rd, 2011

Just a quick post to show, how to loop through all tables and columns in database and find all occurrences of the keyword specified -

/*Declare variables */
Declare @Keyword varchar(200)
Declare @CountTable int
Declare @Loop int
Declare @DbOwner varchar(50)

Set @Loop = 1

/*keyword to search for */
Set @Keyword = report

/*Owner of the object for e.g. dbo.TableName */
Set @DbOwner = ‘dbo.’

/* Temp table to store list of all user tables and column names*/
Declare @UserTables table
(
[Id] int identity(1,1),
[TableName] varchar(300),
[ColumnName] varchar(300),
[DataType] varchar(50),
[Length] int
)

/*Retrieving list of Table Name along with Column, Datatype and length to store in @UserTables table */
Insert into @UserTables
(
[TableName],
[ColumnName],
[DataType],
[Length]
)

Select
sysobjects.name,
syscolumns.name,
systypes.name,
syscolumns.length
From
Sysobjects Join SysColumns on sysobjects.id = syscolumns.id
Join Systypes on syscolumns.xtype = systypes.xtype
Where
/* filtering table list to retrieve only user created tables */
sysobjects.xtype = ‘u’

/* Since we will search string keyword, so will take only text column types */
and systypes.name IN (‘varchar’,'nvarchar’,'ntext’,'text’)

/*Removing extra unwanted table */
and sysobjects.name <> ‘dtpproperties’
order by sysobjects.name, syscolumns.colid

/*Retrieving total user tables records count */
Set @CountTable = (Select @@ROWCOUNT)

Create table #Result
(
Id int identity(1,1),
TableName varchar(300),
ColumName varchar(200),
Result varchar(2000)
)

While @Loop < @CountTable
Begin

Declare @TableName varchar(300)
Declare @ColName varchar(200)
Declare @SqlQuery nvarchar(1000)
Select @TableName = TableName, @ColName = ColumnName from @UserTables where Id = @Loop

Set @SqlQuery = ‘Insert into #Result ‘
Set @SqlQuery = @SqlQuery + ‘ Select ”’ + @TableName + ”’,”’ + @ColName + ”’,’ + @ColName + ‘ from ‘ + @DbOwner + @TableName
Set @SqlQuery = @SqlQuery + ‘ where ‘ + @ColName + ‘ like ”%’ + @Keyword + ‘%”’

/*Executing the SQL Query */
execute SP_ExecuteSql @SqlQuery

Set @Loop = @Loop + 1

End

/*Display final list of table and column where keyword is found */
Select * from #Result

SQL Server -  Keyword 'Report' Search Result

/* Cleanup */
Drop table #Result

Microstrategy Certified Project Designer

June 11th, 2011

Microstrategy Certified Project Designer (CPD)

Microstrategy Certified Project Designer (CPD)

Another challenging day (June 10th, 2011), opted for Microstrategy v9 exams and finally managed to crack it.

For more information on Microstrategy education and certification refer to http://www.microstrategy.com/education/Certification/

Office 2010 – Outlook

May 5th, 2010

This morning I downloaded Microsoft Office 2010 Professional Plus and installed all features. On first preview of OUTLOOK 2010 i liked two new features -

[a] Managing Multiple Email Accounts
In earlier version of Office you could add multiple email accounts and send recieve emails. But it used to mess up in same inbox. Option was to create folders and rules and move the mails to desired folders.

Now in Outlook 2010 :-) accounts have been seperated and inbox is created for each mail account you add.

Outlook

Managing Email Accounts

[b] Outlook Mobile Service (SMS Feature)
You can find a text messaging service provider and setup your account in outlook and send recieve sms as you do the same via your mobile phone.

SMS Service