Click here to Skip to main content
14,539,690 members

Check Validity of SQL Server Stored Procedures, Views and Functions

Rate this:
4.73 (22 votes)
Please Sign up or sign in to vote.
4.73 (22 votes)
13 Feb 2006CPOL
A handy command line tool that checks whether stored procedures, views and functions in a database compile


The other day I was tidying up our SQL database. This involved deleting some obsolete columns. Normally when I do this, I use a SQL script to search for all occurrences of the column I am deleting and then go through them one by one fixing up each stored proc. I remembered that in my small amount of Oracle experience, it was possible to tell in Oracle whether a stored proc was valid. I thought this would be a really handy feature for SQL Server. So the Db Validator was born.

Quick Start

If you aren't interested in the code or building the project, then the output is included in the distribution.
  • Download the source.
  • Modify the dataconfiguration.config file in the bin/debug folder to point at your server and database.
  • Run DbValidator.exe from the command-line. e.g. dbvalidator > c:\output.txt
  • Examine the output. Any compilation errors will be listed together with the error details.

And that's about it. I find this saves a lot of time, and also helps give me confidence that I haven't broken some part of the database without knowing.

How It Works

When I set out to do this, someone reminded me of the "check syntax" feature in SQL Enterprise manager. If I could replicate this functionality, this would be ideal, as I didn't want to actually compile the database objects - just simulate compilation. So I pulled out SQL profiler and recorded the statements that are executed when you press the "Check Syntax" button. Interestingly what happens when you press this button is the following:


I had not come across these SET commands before. Looking them up in BOL tells me what you have already guessed:

  • SET NOEXEC "compiles each batch of Transact-SQL statements but does not execute them"
  • SET PARSEONLY "Checks the syntax of each Transact-SQL statement and returns any error messages without compiling or executing the statement"

Ok, so clearly SET NOEXEC is what I want. So why does Enterprise manager also turn SET PARSEONLY off after it has done the compilation. This I didn't understand, but I decided to replicate exactly what Enterprise manager does in any case.

So, what does our program need to do:

  1. Get a list of all objects in the database that are stored procedures, functions or views
  2. For each object get its definition. i.e. the Create Proc / view / function command
  3. Execute the create command, but wrap the command in the SET NOEXEC / PARSEONLY bits as Enterprise manager did
  4. Detect any errors raised and log them to the Console

Implementation Specifics

On the first pass of this code, I kept getting certain stored procedures that always errored. I figured out that this was due to the fact that they relied on quoted indentifiers being enabled (i.e. statements like the following are allowed: if isnull(@ErrMsg, '') <> ""). Obviously this is sloppy coding and this should really be fixed by replacing the "" with a '', but this case highlighted to me the point that all stored procs etc. are stored in the database together with their ANSI NULL and QUOTED INDENTIFIER settings. In order to have my program to work properly, I needed to figure out what these settings should be. This is what is happening in the following SQL statement:

select name, 
     OBJECTPROPERTY(id, 'ExecIsQuotedIdentOn') as quoted_ident_on, 
     OBJECTPROPERTY(id, 'ExecIsAnsiNullsOn') as ansi_nulls_on, 
     user_name(o.uid) owner 

from sysobjects o
where type in ('P', 'V', 'FN') and 

category = 0

As well as getting the object name, I am getting the QUOTED INDENTIFIER and ANSI NULL settings for the object.

You will also notice from the code that I am using the Enterprise Library data access block. This is a really useful block from the Microsoft Patterns and Practices team. If you don't have the enterprise library installed it should still work, but you will probably find that if you get any data access errors then these may get masked by an error complaining about lack of performance counters. Also note that I am using the June 2005 version of the library.


As you can see, this is a pretty simple application, but I hope you will find it as useful as I do. I guess a future enhancement could be a GUI that displays ticks and crosses against each object, but at the moment it serves my purposes well.


  • 13th February, 2006: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

GeneralMy vote of 4 Pin
Member 1315931528-Apr-17 8:48
MemberMember 1315931528-Apr-17 8:48 
GeneralMy vote of 5 Pin
Member 109931435-Aug-14 5:32
MemberMember 109931435-Aug-14 5:32 
QuestionGood Job Pin
borchanii11-Oct-12 13:06
Memberborchanii11-Oct-12 13:06 
GeneralMy vote of 5 Pin
Kanasz Robert25-Sep-12 22:43
MemberKanasz Robert25-Sep-12 22:43 
GeneralMy vote of 5 Pin
mrsnipey26-Aug-12 18:37
Membermrsnipey26-Aug-12 18:37 
GeneralThanks - here's an equivalent in SQL only [modified] Pin
Danny Shisler16-Jan-10 6:45
MemberDanny Shisler16-Jan-10 6:45 
GeneralRe: Thanks - here's an equivalent in SQL only Pin
MrTelly17-Jan-10 13:39
MemberMrTelly17-Jan-10 13:39 
GeneralRe: Thanks - here's an equivalent in SQL only Pin
Member 454302215-Feb-10 4:28
MemberMember 454302215-Feb-10 4:28 
GeneralRe: Thanks - here's an equivalent in SQL only [modified] Pin
sebwills28-Mar-12 3:35
Membersebwills28-Mar-12 3:35 
GeneralMore checks are possible Pin
Chuckxxx4-Dec-08 8:33
MemberChuckxxx4-Dec-08 8:33 
GeneralReally nice Pin
Evoluteur26-Aug-08 20:32
MemberEvoluteur26-Aug-08 20:32 
GeneralFantastic Pin
sross9021-Aug-08 2:07
Membersross9021-Aug-08 2:07 
GeneralWorks well, with an exception Pin
Mike Renno25-Jul-08 8:30
MemberMike Renno25-Jul-08 8:30 
GeneralAwesome Pin
iffy45612-May-08 15:22
Memberiffy45612-May-08 15:22 
GeneralThank you for putting this together - couple quick tweaks Pin
Emil Lerch22-Apr-08 12:28
MemberEmil Lerch22-Apr-08 12:28 
GeneralRe: Thank you for putting this together - couple quick tweaks Pin
Emil Lerch2-May-08 6:10
MemberEmil Lerch2-May-08 6:10 
GeneralRe: Thank you for putting this together - couple quick tweaks Pin
IPC200014-May-08 2:04
MemberIPC200014-May-08 2:04 
GeneralPerfect for my needs Pin
Guy Harwood11-Jan-07 5:57
MemberGuy Harwood11-Jan-07 5:57 
GeneralJust what I needed. Pin
Nick Hoggard15-May-06 16:50
MemberNick Hoggard15-May-06 16:50 
GeneralRe: Just what I needed. Pin
IPC200016-May-06 10:31
MemberIPC200016-May-06 10:31 
GeneralNice usage Pin
Pedro J. Molina5-Mar-06 3:17
MemberPedro J. Molina5-Mar-06 3:17 
GeneralRe: Nice usage Pin
IPC20005-Mar-06 23:19
MemberIPC20005-Mar-06 23:19 
GeneralInteresting side note Pin
fred h1-Mar-06 13:28
Memberfred h1-Mar-06 13:28 
GeneralRe: Interesting side note Pin
IPC20002-Mar-06 1:33
MemberIPC20002-Mar-06 1:33 
GeneralUse Generate Scripts Pin
w.b.smith24-Feb-06 11:24
Memberw.b.smith24-Feb-06 11:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 13 Feb 2006


42 bookmarked