Version Control your Stored Procedures

  • Comments posted to this topic are about the item Version Control your Stored Procedures

  • Just a small suggestion - please consider EXTENDED properties for DB objects (including Stored Procedures), by using them in a key-value fashion you could avoid a lot of text processing.

    Regards from DownUnder.

  • Just a small concern...

    As far as experience and memory serves, sp_helptext is not a reliable source for recreating SQL server objects. SQL management objects are the preferred source.

    Changes to an object and/or metadata may render sp_helptext outdated - the script used to create the original object may not reflect the object in its current state. I seem to remember a case where the sp_helptext record was blatantly incorrect after a (?) designer edit.

    Does anyone have a concise view of when this may occur, and whether stored procedures are for practical purposes immune to these cases?

  • I really enjoy seeing scripts like this, since the whole topic of scripting is fascinating. What I really like about this is that someone has gone ahead and brewed a solution that fits their requirements like a glove. It is neat, because it will work with any version of SQL Server 2000+.

    I wouldn't like anyone to think that this is a recommended general solution, though. SMO is the way to go! (I still occasionally use DMO)

    Best wishes,
    Phil Factor

  • I also had similar issues with sp_helptext and eventually found a way of scripting objects using

    Microsoft.SqlServer.Management.Smo.Scripter in Powershell.. this has the advantage that it handles all types of objects including everything from logins to linked-servers, mail profiles, etc, and can take advantage of easy iteration possible in powershell.

    I've written a skeleton (and, disclaimer; is not warranted in any way) ps script that captures relevant source/schema stuff in SQL Server, if anyone's interested. Obviously it can be adjusted to taste for extended properties etc (which I turn off by default) and should be tweaked to expose specific properties relevant to only certain objects.

    Link here

    http://wp.me/pje2P-l

    I also have a wrapper script that iteratively runs it for each database on the server, and a server-objects script that captures mail profiles, accounts etc, for anyone interested.

    Regards,

    Mike

  • I'm with Phil on this one. It was a good read and an interesting solution, but way more difficult than needed since SMO can do so much of the work.

    That said, yee hah! for so much discussion around getting database code into source control. I think this is a huge whole in lots of shops these days.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • Or you can simply use ApexSQL Edit, which has some very spiffy built-in source code control functionalilty - among many other features.

    Disclaimer: I have a close relationship with Apex, use their products and recommend them to my clients. Also, if you care to you can mention TheSQLGuru sent you you will get a discount and my daughter will get a few coins for her college fund.

    Best,
    Kevin G. Boles
    SQL Server Consultant
    SQL MVP 2007-2012
    TheSQLGuru on googles mail service

  • Good article. I always like to see/hear how others are using version control on their databases. Currently, we version all objects not just stored procedures, functions and triggers. There are inherent issues with this obviously, but we have tried to figure out ways around them that fit our particular needs. We are even working on a way to create automatic builds of our database changes to a testing environment without using compare tools. I'd love to hear if others have successfully used a version control solution to maintain database updates as well.

    Thanks again for the article

    J. Baxter

  • Automated deployments of stored-procs is relatively easy, but anything that involves a table schema change for example kind of makes the whole approach a bit complex and prone to problems. Also, it becomes much harder if you are building routines that need to be created in a specific order- we've toyed with creating dependencies but never got it working satisfactorily.

    More useful for us in our small shop was detecting unauthorised/unexpected changes to our database objects, and providing a good audit of objects altered to tie into our change control process.

    Using SMO we've been able to version most objects quite easily and build a daily source copy and an associated job to compare the output to a VC'd baseline, noting differences.

    It's also made capturing changes between a development environment and production environment a lot easier- script both to two folders and run a winmerge or similar comparison tool to see what ammendments have been made- although as noted above, syncing the changes almost always is easier to handle the "long" way for our relatively simple db.

  • I have to agree with some of the comments here particularly on the principal on version control however and at the risk or creating some controversy, I would question the value of the solution provided other than it fitted exactly what one person required and it managed to generate some discussion.

    Phil was spot on with recommending that SMO was the way to go and while I agree with that for this particular problem my underlying point is that the solution to a problem should really be based on sound technical knowledge of the tool set and the most appropriate elements of that tool set to be used to provide the solution.

    Powershell was also mentioned and this in conjunction with SMO/SQLPSX can give you a scripter in one line of code. You cant get much simpler than that and while not very useful as it stands it does serve as an example of the value in knowing what is available.

    -- Script all tables in the adventureworks database on the default instance on the local server

    Get-SqlDatabase "(localserver)" "adventureworks" | Get-SqlTable | Get-SqlScripter

    Articles like this just make me question just how much knowledge the writer has with the tool set we all use on a day to day basis.

  • We had a similar solution to the problem of developers changing stored procedures without any version control. The only version control we had was limited to the previous backup.

    The solution: I built a .net console app, used SMO to script each object to a text file, then checked in each file into visual source safe. The app runs every night, does a check-in for multiple servers/databases. We setup a source safe project for every database. When we do a release, we apply a label on the whole project/database.

    Now when a developer makes a change to a stored procedure, that change is checked into source safe that night. Using source safe we can track the change history on each procedure, table, trigger, etc.

  • I personally haven't found a great deal of info on how to script objects using SMO which is why I think people end up doing slightly scary things like querying syscomments and other arcane solutions. From the snippets on the net we've been able to sort of work SMO out but it always seems to require a few extra steps of understanding to make use of, e.g. smo is a c# library seemingly, accessible from powershell, but could equally be rolled into a CLR storedproc.

    I've hated having to make uninformed choices as to where run this sort of stuff from or how to architect it to "best practice" quality standards.

    Does anyone have a book to recommend that delves into the powershell scripting side of sql server more?

  • mike.renwick-894639 (5/6/2010)


    I personally haven't found a great deal of info on how to script objects using SMO which is why I think people end up doing slightly scary things like querying syscomments and other arcane solutions. From the snippets on the net we've been able to sort of work SMO out but it always seems to require a few extra steps of understanding to make use of, e.g. smo is a c# library seemingly, accessible from powershell, but could equally be rolled into a CLR storedproc.

    I've hated having to make uninformed choices as to where run this sort of stuff from or how to architect it to "best practice" quality standards.

    Does anyone have a book to recommend that delves into the powershell scripting side of sql server more?

    Yep, "SQL Server 2008 Administration with PowerShell" by Anathakumar Muthusamy & Yan Pan.

    I'm sure I mispelled one of those names.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • anything that involves a table schema change for example kind of makes the whole approach a bit complex and prone to problems

    Right on the nail. If you can crack the problem of source control for tables, then you're probably home and dry. This is a problem that is ridiculously hard. Tables don't, and most certainly can't, have a source of the same type as a C# source, or a stored procedure. You can't compare tables merely from scripts. I can generate the same table using scripts that are really quite different in the way that they represent the table and its associated components, and I can reverse-engineer a table-build script in a number of different ways. The only effective way to detect a difference between two databases is to compare the metadata. Tables have a whole lot of dependent columns, indexes, constraints and triggers. How do you relate this lot together in the source-control model? Is a table, together with all its dependent components, one single object? If so, how come you can change it without requiring to check out, or 'reverse-engineer', the 'source'? How do you deal with something as potentially complex as a trigger or check constraint, which surely needs a separate identity in source control? (..and so on.. and so on ...) So often, folks move over from traditional programming and wonder why source-control is all so different with SQL Databases. It can be done, but it takes a great deal of head-scratching to get it right.

    Best wishes,
    Phil Factor

  • I agree with a lot of users here that Powershell and SMO are much much better choices.

    Mike.Renwick..i will look into your Powershell code...its a great start.

    Jon Mcghee...is it possible you could share your code?

    For other folks who are not inclined on spending too much time on PS/SMO, SQL 2008 has a built in task which can generate SP script file. A bunch of clicks but it gets the job done for light usage DBs

    It is also available as a free utility for previous versions.

Viewing 15 posts - 1 through 15 (of 76 total)

You must be logged in to reply to this topic. Login to reply