Rodney

sp_helptext squared

Published Monday, April 09, 2007 9:13 PM

As I sat at my desk, like most days, pondering what a partial database restore truly is in regards to object-level restores and filegroups and noting that though this sentence will go on longer than I intended I will not finish it prior to finishing the initial thought, my phone rang and the question came to the DBA, me, "How can I search for code in a view or stored procedure that contains a specific string?".

Here I ponder weak and weary...

It should have been obvious...the answer should have come immediately, however,with a bit of time to spare, I explained I would return the call momentarily as I had another critical issue to deal with.

So, how does one do what was asked in the first long sentence of this entry?

I knew that the "sysobjects" table contained the objects themselves: views, stored procedures, tables, etc. But where was the actual code that defined these objects?  Then it came to me:

sp_helptext

This little gem of a system stored procedure will return the code for which my interogator wanted to search. So where does sp_helptext get its info? Again, simple:

try:

sp_helptext sp_helptext

In the bowels of the sp_helptext stored procedure itself was revealed the missing table:

syscomments

So the query issued forth that would expose the TEXT field which holds the answer. A join from sysobjetcs to syscomments on the ID column was all that was required.

Throw in criteria for the XTYPE field, and you can narrow your search to a View(V) or Stored Procedure (P).

Fianlly, the impetus for a Reporting Services report was born. One that can pass in a parameter as the object name with other parameters for database and even server that would control the data source itself via an expression. One would only have to run the report, pass in the search string and the report would show not only the objects that contained the string, but the code too. This report, like the cat in the box or the winning numbers of a lottery ticket do not exist yet. But...

I will make this report if there is interest in it. I will make it and I will distribute the RDL for all who have read this far and agree there is a need. I can even have it posted by this Friday. I work well under deadline.

Rodney

Author "Pro SQL Server 2005 Reporting Services"

 

by Rodney

Comments

 

Phil Factor said:

see my splendid blog entry 'spWhereIsItUsed: Leaves from a programmers notebook ' on this site for a stored procedure  to answer the DBA's request!
http://www.simple-talk.com/community/blogs/philfactor/archive/2006/06/03/854.aspx

It might need a tweak in order to do Views, I'll admit, but it is a one-liner
April 10, 2007 4:49 AM
 

Rodney said:

Phil,
Splendid indeed. Apologies for the concentric circles of thought.  Might I make use of your solution for a Reporting Services report, giving you full content credit? This brings up another question I had about the need for Reporting Services content on this site.  
Must get to reading all prior blog entries.
April 10, 2007 7:19 AM
 

Phil Factor said:

Rodney,

Occasionally I feel the urge to write something practical in my Blog, though generally I like to avoid such things.

We definitely need more about Reporting Services on the site. It is one of the most popular topics. Please feel free with adapting the code. I use it often myself and it certainly saves time when developing database applications.
April 10, 2007 11:26 AM
You need to sign in to comment on this blog

















<April 2007>
SuMoTuWeThFrSa
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345
Go With the Flow
 Knowing enough about the routes that messages take is vital to being an effective Exchange admin,... Read more...

When Email Collaboration Could Have Changed History
 In our mission to make history relevant to the busy IT executive, we speculate how Email might have... Read more...

Bunnikins!
 When an IT manager is selected as a victim of office politics of a large corporate, it is time for him... Read more...

Exchange Database Technologies
 One of the most misunderstood technologies in Exchange Server, regardless of its version, is the... Read more...

Top Tips for Exchange Admins
 Michael Francis hands out imaginary Olympic medals to the winner of the August 'Top Tips for Exchange... Read more...