# Wednesday, November 09, 2011
« Simple Membership MVC3 1.2 available on ... | Main | Using MVCContrib 3 Test Helpers with ASP... »

I don’t know about you but every SQL Server environment I’ve ran into is extremely well documented and easy to figure out the dependencies.wakeup



…And then I wake up.



Documented SQL Servers?

Ever have a question about where in your SQL Server stored procedures you used that particular piece of text?

Maybe you need to know where you used that CLR integrated function call in your SQL Server code base?

I recently began a new job and while getting myself up to speed on their, I mean our database environment I discovered that the server had CLR integrated functions.  Where are these being used? Nobody on the team could give me a straight answer.

sp_MSForeachdb to the rescue

This built-in little gem (undocumented) will loop through all your databases on a server.  It takes in a string as a command with the ? as a placeholder for the table name.

sp_MSForeachdb ' use ?; SELECT DISTINCT ''?'' as DbName, o.name AS OBJ_Name, o.type_desc FROM sys.sql_modules m INNER JOIN sys.objects o ON m.object_id=o.object_id WHERE (m.definition LIKE ''%YourTextToFind%'' )'

This will return an empty result set if the database does not have YourTextToFind in any of it’s stored procedures, views and functions.

When it does find it will look something like:

DbName OBJ_Name type_desc

Now you are armed with a list of usages/dependencies.  It’s a simple man’s database documentation tool. 

I imagine the above code could be better written.  After all, I’m just an accidental DBA. Smile

Here’s some cool usages floating around on the interwebs.

Find the space used for each database.

Six common uses of sp_MSforeachdb.

There’s also sp_MSforeachtable….  I think you can guess just what you might do with that.

Happy Coding!

Posted 11.09.2011  #    Comments [0]  |  Related posts:

Please login with either your OpenID above, or your details below.
(will show your gravatar icon)
Home page

Comment (Some html is allowed: b, blockquote@cite, em, i, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

[Captcha]Enter the code shown (prevents robots):

Live Comment Preview