Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12345»»»

Version Control your Stored Procedures Expand / Collapse
Author
Message
Posted Thursday, May 6, 2010 7:18 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Thursday, February 20, 2014 12:27 PM
Points: 83, Visits: 597
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.
Post #916926
Posted Thursday, May 6, 2010 7:27 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, March 31, 2014 8:17 AM
Points: 110, Visits: 182
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?
Post #916934
Posted Thursday, May 6, 2010 7:31 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Tuesday, November 18, 2014 3:50 AM
Points: 13,991, Visits: 28,373
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
The Scary DBA
Author of: SQL Server Query Performance Tuning
SQL Server 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #916943
Posted Thursday, May 6, 2010 7:33 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Monday, November 10, 2014 12:04 PM
Points: 590, Visits: 2,565
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
Simple Talk
Post #916948
Posted Thursday, May 6, 2010 7:35 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, August 6, 2014 11:38 AM
Points: 24, Visits: 129
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.
Post #916951
Posted Thursday, May 6, 2010 7:41 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: 2 days ago @ 8:39 AM
Points: 338, Visits: 1,437
irozenberg (5/6/2010)
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.


I'm not sure what you mean?
Post #916960
Posted Thursday, May 6, 2010 7:44 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: 2 days ago @ 8:39 AM
Points: 338, Visits: 1,437
jcaradocdavies (5/6/2010)
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?


The only problem I've found is that on rare occasions it does odd things with line breaks.
These become obviously evident when you try to run the script as the script will fail.
Post #916961
Posted Thursday, May 6, 2010 7:46 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: 2 days ago @ 8:39 AM
Points: 338, Visits: 1,437
mike.renwick-894639 (5/6/2010)
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


The advantage of the method I've used is that it also creates a standard header for the script and in the version I use I add a call to another stored proceddure that writes to a ChangeCatalogue table with details of when the stored procedure was installed, the version and who ran the script.
Post #916964
Posted Thursday, May 6, 2010 7:48 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: 2 days ago @ 8:39 AM
Points: 338, Visits: 1,437
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?

I don;t know much about SMO either. I think it's something I'll look into though.
Post #916965
Posted Thursday, May 6, 2010 7:54 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: 2 days ago @ 8:39 AM
Points: 338, Visits: 1,437
Phil Factor (5/6/2010)
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.


Yes that's one of the tougher areas of deployment of changes. We have a sequentially numbered set of 'incremental' scripts for DDL changes. All scripts are rerunable so for example if a new column is to be added they check for existance before trying to add the column, this means that script scn be rerun without causing errors e.g.:

IF NOT EXISTS(SELECT 1
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'myTable'
AND COLUMN_NAME = 'mycol1')
BEGIN
ALTER TABLE dbo.myTable
ADD mycol1 int NULL
...

This means that if the incremental files are all run in the right order the correct changes to the database should be made.
Post #916978
« Prev Topic | Next Topic »

Add to briefcase ««12345»»»

Permissions Expand / Collapse