Click here to Skip to main content
6,629,377 members and growing! (22,101 online)
Email Password   helpLost your password?
Database » Database » SQL Server     Beginner License: The BSD License

Check Validity of SQL Server Stored Procedures/Views/Functions (updated)

By Emil Lerch

Command line tool to check validity of objects in SQL Server databases
SQL, C# 2.0.NET 2.0, Win2K, WinXP, Win2003, Vista, .NET 3.0, .NET 3.5, ADO.NET, SQL 2000, SQL 2005, VS2008
Posted:2 May 2008
Updated:19 Sep 2008
Views:17,370
Bookmarked:30 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
10 votes for this article.
Popularity: 3.84 Rating: 3.84 out of 5
2 votes, 20.0%
1

2
2 votes, 20.0%
3
2 votes, 20.0%
4
4 votes, 40.0%
5

Introduction

This is an updated version of the code posted by IPC2000. The following changes have been made:

  • References to the enterprise library have been removed, allowing for more flexibility in specifying the connection string. Connection strings and verbosity can be specified on the command line.
  • Output is more "spreadsheet friendly", using tab delimited output that can be copy/pasted into Excel
  • Support for SQL 2005 has been introduced. Separation of user from schema in SQL 2005 requires a slightly different SQL statement.
  • Validation logic has been moved to a separate class which allows for referencing/use in another project. This is especially handy for inclusion as an NUnit test.

Background

Please refer to this article for more background on the validation. This is simply a polished version of that piece.

Using the Code

The console executable can be run specifying a /c:<connection string> argument as well as a /v:<verbosity> argument. Verbosity values are:

  • Quiet: Designed for spreadsheets, a tab delimited list of invalid objects
  • Normal (default): Includes some description of what's going on. Version of SQL Server detected and a summary of objects processed and object failures
  • Verbose: Code will list every object processed as it's processing it 

Update

A new switch, /x, has been added to ask the program to actually run any procedure, function, view it thinks is safe.  If CREATE, DROP, INSERT, UPDATE, DELETE, or EXEC/EXECUTE appears in the text of the object it will move on, otherwise, it will add a dummy value for each parameter it finds and execute the object.  Since the logic isn't perfect (stored procs can call functions that do bad things), this is turned off by default, but enabling it should generally be safe if the system was coded using best practices.  This feature has limited testing behind it - please comment below if you find issues and I'll take a look when I can.

You can also reference the executable directly into NUnit and run the corresponding method. Here's an example C# test method:

[TestFixture]
public class DatabaseValidation
{
    [Test]
    public void CheckForDatabaseCompilationProblems()
    {
	Assert.IsTrue(
	DatabaseValidator.Validator.DatabaseIsValid(
	ConfigurationManager.ConnectionStrings
	[MyConnectionString].ConnectionString,
	DatabaseValidator.Verbosity.Normal),
	"Invalid objects found in the database - Run SqlValidator");
    } 
} 

History

  • 2008-05-02: Initial post

License

This article, along with any associated source code and files, is licensed under The BSD License

About the Author

Emil Lerch


Member

Location: United States United States

Other popular Database articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 12 of 12 (Total in Forum: 12) (Refresh)FirstPrevNext
GeneralRuns on SQL 2008 But does not catch everything PinmemberVaccano14:41 25 Mar '09  
GeneralConnection string and SSPI Security context [modified] PinmemberC.D. Hilton5:11 23 Sep '08  
GeneralNice! Pinmembermsmits20:55 26 May '08  
GeneralSome welcome additions... Pinmembermsmits21:07 26 May '08  
GeneralRe: Some welcome additions... PinmemberEmil Lerch14:00 19 Sep '08  
GeneralVery useful/clever Pinmemberpap196421:35 6 May '08  
GeneralRe: Very useful/clever PinmemberWil Hutton6:28 19 May '08  
GeneralRe: Very useful/clever PinmemberEmil Lerch8:50 19 May '08  
General[Message Removed] PinmemberMojtaba Vali21:31 2 May '08  
GeneralHorizontal Scrolling PinmemberChris Meech7:34 2 May '08  
GeneralRe: Horizontal Scrolling PinmemberEmil Lerch7:42 2 May '08  
GeneralRe: Horizontal Scrolling PinmemberChris Meech10:30 2 May '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 19 Sep 2008
Editor: Deeksha Shenoy
Copyright 2008 by Emil Lerch
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project