Home > SQL Server Tips > Database Administration > SQL Server version control
SQL Server Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

DATABASE ADMINISTRATION

SQL Server version control


Hilary Cotter, Contributor
06.02.2005
Rating: -4.14- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


Like the weather, SQL developers and database administrators talk about version control, but most of them don't do anything about it. Version control means managing the versions of the objects created and deployed in their SQL Server environments. Typically, this involves developers and DBAs checking scripts including object creation scripts into Visual Source Safe (VSS), or a similar version control software, when they create them and then checking them out and back in again when they make modifications.

Several benefits come with establishing version control:

The problem with SQL Server version control is that the tool of choice that most SQL developers and DBAs use to build their database objects is Query Analyzer. Query Analyzer does not have Visual Source Safe Integration built into it. In SQL 2005, SQL Server Management Studio is integrated with Visual Source Safe, so a developer using SQL Server Management Studio can check his objects in and out of Visual Source Safe.

Ken Henderson's book, The Guru's Guide to SQL Server Stored Procedures, XML, and HTML, shows how to integrate Query Analyzer with Visual Source Safe, but it still requires SQL developers to rigorously check the objects they are modifying in and out through Query Analyzer using a few simple keystrokes.

There are commercially available tools that will integrate an IDE (like Enterprise Manager or SQL 2005 Server Management Studio) with Visual Source Safe. Query Google or MSN Search for SQL IDE Visual Source Safe for a list of such products.

The advantages of using an IDE that integrates with Visual Source Safe (or another version control software product) are:

(This list is courtesy of SQL Server MVP Erland Sommarskog. Any errors in this list are solely my own, any gems you may find in this list are completely Erland's.)

In well-disciplined shops there are well-established protocols and procedures for checking objects in and out of the co


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED CONTENT
Microsoft SQL Server Performance Monitoring and Tuning
Performance implications of transaction log autogrowth in SQL Server
The short course on how SQL Server really works
Determining the source of full transaction logs in SQL Server
Improving SQL Server full-text search performance
New GROUP BY option provides better data control in SQL Server 2008
Microsoft SQL Server 2008 Resource Governor primer
Examining data files when SQL Server tempdb is full
Testing transaction log autogrowth behavior in SQL Server
Meeting business needs with SQL Server full-text search
Using dynamic management views to improve SQL Server index effectiveness

SQL Server Database Modeling and Design
Managing the development lifecycle with Visual Studio Team System 2008
A first look at Visual Studio Team System 2008 Database Edition
Testing transaction log autogrowth behavior in SQL Server
Top 10 SQL Server Tips of 2008
Tutorial: SQL Server indexing tips to improve performance
Tutorial: Learn SQL Server basics from A-Z
SQL Server database design disasters: How it all starts
Can you shrink your SQL Server database to death?
Physical data storage in SQL Server 2005 and 2008
Enforcing data integrity in a SQL Server database

Database Administration
Top load balancing methods for SQL Server
Performance implications of transaction log autogrowth in SQL Server
The keys to database backup protection for SQL Server
Understanding transparent data encryption in SQL Server 2008
Working with sparse columns in SQL Server 2008
Determining the source of full transaction logs in SQL Server
Implementing SQL Server 2008 FILESTREAM functionality
Improving SQL Server full-text search performance
Using the OPENROWSET function in SQL Server
New replication features in SQL Server 2008 and what they mean to you

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
contiguity  (SearchSQLServer.com)
contiguous  (SearchSQLServer.com)
drilldown  (SearchSQLServer.com)
hashing  (SearchSQLServer.com)
hybrid online analytical processing  (SearchSQLServer.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary


mpany's version control software. DBAs and developers rigorously check the objects they will be modifying in and out of Visual Source Safe or other version control software.

I have not had the luxury of working for an institution that has such protocols and procedures or, for the most part, even enterprise-level version control software. So I have had to deal with DBAs and SQL developers begging me to restore their databases to recover an earlier version of a stored procedure, view or function. For larger databases, that can be a lengthy process. Frequently the SQL developer has been working in a development environment that is not backed up, and recovering these objects is not possible.

In such undisciplined environments, I implement a nightly batch job that connects to all SQL Servers in the environment, scripts out all database objects and then pushes the modified objects into Visual Source Safe.

In Visual Source Safe, I can easily retrieve a previous version of an object. I can also label groups of objects so that I can generate an installation script for a set of objects for a specific version. For instance, I can label all objects Release to Manufacturing (RTM), then a group of objects later down the road as SP1, and so on. Within Source Safe, I can check out all objects labeled SP1 and generate a script to distribute to my customers.

The nightly batch job also keeps a run log of error messages generated during the run for debugging purposes. I can use this run log to improve my scripts by fixing the errors or working around particular problems that cause my script to fail.

I also generate a log of what has changed in the environments. Then I can evaluate the code that is going into the test, QA, release or production environments to determine whether:

All objects are tagged with extended properties, which include versioning information. With this versioning information I can query an environment and ensure that all objects are at a specific version and then detect which objects belong to a different version.

At the completion of the nightly job, I e-mail the logs to myself for evaluation.

The logs and the version control information provide high visibility to what has changed in your environments, and they allow you to proactively react to what will be deployed in the environments downstream. The most compelling reason for implementing such a version control process, in my opinion, is to have a repository of all the stored procedures in my environment so I can run scripts against the repository to check for poor coding practices. I use slightly modified scripts that I obtain from Linchi Shea's Real World SQL Server Administration with Perl to do this checking. I check for cursor use, temp table creation, object references without an owner name and other poor coding practices. A database that scores high for bad coding practices will be evaluated in greater depth.

Here is a short description of how this code works.

There are several parts to this VSS agent:

I use blat to e-mail these logs to myself because the security policy in most of the companies I work in prevent using SQL Mail or installing SMTP on their SQL Servers.

I schedule a job to run via SQL Scheduler. It runs an operating system command, which is a batch file that looks like this:


where Server1, Server2 and Server3 are SQL Servers in my environment that I want to bring under version control. Modify your batch file to include the names of your SQL Servers.

In my VSSagent script I start off with several constants that define where my file system repository is -- and the path, account and password for Visual Source Safe.

You will have to modify this for your particular environments. The run log will be placed in your FileRepositoryPath and will be called RunLogMM-DD-YYYY.log. The Difference log will be called DifferenceLogMM-DD-YYYY.log.log and will also be placed in the FileRepositoryPath.

This script will create project paths in VSS and file system paths for each server in the file system. By default the project path in VSS will be in the root of your VSS database. SQL Server Instances have the in their name replaced with an underscore. For instance, a SQL Server Instance called SQLServerInstanceName would be called SQLServer_InstanceName in the file system. That is because we use separate subdirectories for each SQL server and its databases in the file system, and an instance name would be recognized as a database of a SQL Server.

The script will then connect with user databases, check to see if that database is under source control and, if not, place it under source control by creating a separate sub project for that SQL Server in VSS. It will then enumerate the objects, add them to VSS if they are not under source control, check the changes if there have been changes and note that a difference has been detected for this object for that SQL Server in that database. It also tags each object with version control information using extended properties, which allows us to use the system function fn_listextendedproperty to determine which objects are at what version in our database.

Please find in the attached script the VSSAgent code. Feel free to modify it to implement your own version control for SQL Server objects.


Hilary Cotter has been involved in IT for more than 20 years as a Web and database consultant. Microsoft first awarded Cotter the Microsoft SQL Server MVP award in 2001. Cotter received his bachelor of applied science degree in mechanical engineering from the University of Toronto and subsequently studied both economics at the University of Calgary and computer science at UC Berkeley. He is the author of a book on SQL Server transactional replication and is currently working on books on merge replication and Microsoft search technologies.

Rate this Tip
To rate tips, you must be a member of SearchSQLServer.com.
Register now to start rating these tips. Log in if you are already a member.


Submit a Tip




DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



SQL Server Development - .NET, C#, T-SQL, Visual Basic
HomeNewsTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersIT Downloads
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2005 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts