About the author

Steven HarmanSteven Harman is a passionate developer who believes that writing great software isn't just a job, its a craft.

ASP.NET MVP

For recent posts and more about me, scroll to the bottom.

Subscribe

  • Subscribe to my feed. via RSS
  • Subscribe via email via email

Jobs

Badges

  • Subtext Project
  • Support Subtext
  • HiddenNetwork.com Banner

Another Bulletproof SQL Script Using INFORMATION_SCHEMA Views

Bullet Proof

As I'm sure many of you know, peer pressure can be a very powerful motivator... and I am not immune to such pressure. So, hot on the heels of Phil and Jon's posts on the magic voodoo that can be accomplished with the INFORMATION_SCHEMA Views, I felt obligated to add my own small bit of wisdom to the fray.

In reading through Phil's article I noticed he didn't mention anything in particular about the creating, modifying, and/or dropping of Stored Procedures when working with a common schema among a development team. This is one thing we often have to deal with on the subText team... so what better opportunity to share than right now!

Keeping those Stored Procs in Check

In the following example we want to check for the existence of the subtext_ClearBlogContent stored procedure in the dbo schema. If we find said stored procedure, we want to drop it so that later we can create a new one (that may or may not have been changed since the last time we built our database). The script to do so is as follows:

if exists 
  (select ROUTINE_NAME from INFORMATION_SCHEMA.ROUTINES 
      where ROUTINE_TYPE = 'PROCEDURE' 
      and OBJECTPROPERTY(OBJECT_ID(ROUTINE_NAME), 'IsMsShipped') = 0 
      and ROUTINE_SCHEMA = 'dbo' 
      and ROUTINE_NAME = 'subtext_ClearBlogContent')
drop procedure [dbo].[subtext_ClearBlogContent] 
GO

The important things to note here are that (1) we only want to look for (and possibly drop) procedures that did not come installed with SQL Server, and (2) we are looking in a particular schema (the dbo schema) for the stored procedure. We can easily make this script more flexible by adding a couple of template parameters like so:

if exists 
  (select ROUTINE_NAME from INFORMATION_SCHEMA.ROUTINES 
      where ROUTINE_TYPE = 'PROCEDURE' 
      and OBJECTPROPERTY(OBJECT_ID(ROUTINE_NAME), 'IsMsShipped') = 0 
      and ROUTINE_SCHEMA = '<dbUser,varchar,dbo>' 
      and ROUTINE_NAME = 'subtext_ClearBlogContent')
drop procedure [<dbUser,varchar,dbo>].[subtext_ClearBlogContent] 
GO

Notice the two lined where I've bolded <dbUser,varchar,dbo>. This would allow us to define the particular schema at run time. Don't forget, that to run the second example in Query Analyzer, you'll need to hit CTRL+SHIFT+M.

UPDATE: I had a slight typo in the original example scripts where I had square brackets around the variable in the and ROUTINE_SCHEMA = 'dbo' lines. The are fixed and working beautifully now!

What others are saying.

# Another Bulletproof SQL Script Using INFORMATION_SCHEMA Views
Gravatar DotNetKicks.com
Jul 25, 2006
You've been kicked (a good thing) - Trackback from DotNetKicks.com
# re: Another Bulletproof SQL Script Using INFORMATION_SCHEMA Views
Gravatar Haacked
Jul 25, 2006
Now if we could just get SQL Server 2005 to generate this when creating its create/drop scripts.
Comments have been closed on this topic.