<p>Can anyone provide some real examples as to how best to keep script files for views, stored procedures and functions in a SVN (or other) repository.</p>

<p>Obviously one solution is to have the script files for all the different components in a directory or more somewhere and simply using TortoiseSVN or the like to keep them in SVN, Then whenever a change is to be made I load the script up in Management Studio etc. I don't really want this.</p>

<p>What I'd really prefer is some kind of batch script that I can run periodically (nightly?) that would export all the stored procedures / views etc that had changed in a given timeframe and then commit them to SVN.</p>

<p>Ideas?</p>

[{'answer_id': 51294, 'author': 'Matt', 'author_id': 5024, 'author_profile': 'https://Stackoverflow.com/users/5024', 'pm_score': 2, 'selected': False, 'text': '<p>I find it best to treat Stored Procedures just like any other compilable code:  Code lives in the repository, you check it out to make changes and load it in your development tool to compile or deploy the code.   </p>\n'}, {'answer_id': 51295, 'author': 'Nick Stinemates', 'author_id': 4960, 'author_profile': 'https://Stackoverflow.com/users/4960', 'pm_score': 4, 'selected': True, 'text': "<p>Sounds like you're not wanting to use Revision Control properly, to me.</p>\n\n<blockquote>\n  <p>Obviously one solution is to have the\n  script files for all the different\n  components in a directory or more\n  somewhere and simply using TortoiseSVN\n  or the like to keep them in SVN</p>\n</blockquote>\n\n<p>This is what should be done. You would have your local copy you are working on (Developing new, Tweaking old, etc) and as single components/procedures/etc get finished, you would commit them individually until you have to start the process over.</p>\n\n<p>Committing half-done code just because it's been 'X' time since it was last committed is sloppy and guaranteed to cause anyone else using the repository grief.</p>\n"}, {'answer_id': 51306, 'author': 'Ash', 'author_id': 5023, 'author_profile': 'https://Stackoverflow.com/users/5023', 'pm_score': 0, 'selected': False, 'text': '<p>Not sure on your price range, however <a href="http://www.innovartis.co.uk/" rel="nofollow noreferrer">DB Ghost</a> could be an option for you. </p>\n\n<p>I don\'t work for this company (or own the product) but in my researching of the same issue, this product looked quite promising.</p>\n'}, {'answer_id': 51314, 'author': 'Whisk', 'author_id': 908, 'author_profile': 'https://Stackoverflow.com/users/908', 'pm_score': 1, 'selected': False, 'text': '<p>I\'d recommend <a href="http://www.red-gate.com/" rel="nofollow noreferrer">Redgate</a> SQL Compare for this - it allows you to compare database versions and generate change scripts - it\'s also fairly easily scriptable.</p>\n'}, {'answer_id': 51322, 'author': 'Ivan Hamilton', 'author_id': 4729, 'author_profile': 'https://Stackoverflow.com/users/4729', 'pm_score': 2, 'selected': False, 'text': '<p>You can create a batch file and schedule it:</p>\n\n<ul>\n<li>delete the contents of your scripts directory</li>\n<li>using something like <a href="http://www.codeplex.com/ExportSQLScript" rel="nofollow noreferrer">ExportSQLScript</a> to export all objects to script/scripts</li>\n<li>svn commit</li>\n</ul>\n\n<p>Please note: That although you\'ll have the objects under source control, you\'ll not have the data or it\'s progression (is that a renamed field, or 1 new field and 1 deleted?).</p>\n\n<p>This approach is fine for maintaining change history. But, of course, you should never be automatically committing to the "production build" (unless you like broken builds).</p>\n\n<p>Although you didn\'t ask for it: This approach also won\'t produce a set of scripts that will upgrade a current DB. You\'ll only have initial creation scripts. Recording data progression and creation upgrade scripts is beyond basic source control systems.</p>\n'}, {'answer_id': 51364, 'author': 'Scott Bennett-McLeish', 'author_id': 1915, 'author_profile': 'https://Stackoverflow.com/users/1915', 'pm_score': 0, 'selected': False, 'text': "<p>I should've been a little more descriptive. The database in question is for an internal ERP system and thus we don't have many versions of our database, just Production/Testing/Development. When we've done a change request, some new fancy feature or something, we simply execute a script or series of scripts to update the procedures in question on the Testing database, if that is all good, then we do the same to Production.</p>\n\n<p>So I'm not really after a full schema script per se, just something that can keep track of the various edits to the stored procedures over time. For example, PROCESS_INVOICE does stuff. It gets updated in some minor way in March. Some time later in say May it is discovered that in a rare case customers get double invoiced (or some other crazy corner case). I'd like to be able to see what has happened over time to this procedure. Currently the way the development environment is setup here I don't have that, which I'm trying to change.</p>\n"}, {'answer_id': 51371, 'author': 'Kociub', 'author_id': 3632, 'author_profile': 'https://Stackoverflow.com/users/3632', 'pm_score': -1, 'selected': False, 'text': '<p>You can always try integrating SourceSafe with SQL Server. Here\'s a quick start : <a href="http://msdn.microsoft.com/en-us/library/ms181089(VS.80).aspx" rel="nofollow noreferrer">link</a> . To work with it you\'ve got to have Managment Studio Developers Edition.</p>\n'}, {'answer_id': 52536, 'author': 'Dr Zimmerman', 'author_id': 4605, 'author_profile': 'https://Stackoverflow.com/users/4605', 'pm_score': 0, 'selected': False, 'text': '<p>I can recommend DBPro which is part of Visual Studio Team Edition.  Have been using it for a few months for storing all parts of the database in Team Foundation Server as well as for deployment and database compares, etc. </p>\n\n<p>Of course, as someone else mentioned, it does depend on your environment and price range.</p>\n'}, {'answer_id': 55727, 'author': 'Josef', 'author_id': 5581, 'author_profile': 'https://Stackoverflow.com/users/5581', 'pm_score': 0, 'selected': False, 'text': '<p>I wrote a utility for dumping all of the relevant parts of my db into a directory structure that I use SVN on. I never got around to trying to incorporate it into the Manager but, if you\'re interested, it\'s here: <a href="http://www.reluctantdba.com/dbas-and-programmers/sqltools/svnforsql2005.aspx" rel="nofollow noreferrer">http://www.reluctantdba.com/dbas-and-programmers/sqltools/svnforsql2005.aspx</a></p>\n\n<p>It\'s free and, since I regularly run it, you know any bugs get fixed quickly.</p>\n'}, {'answer_id': 150034, 'author': 'Josef', 'author_id': 5581, 'author_profile': 'https://Stackoverflow.com/users/5581', 'pm_score': 1, 'selected': False, 'text': '<p>Based on your expanded question, you really want to use DDL triggers. Check out <a href="http://www.sqlteam.com/article/using-ddl-triggers-in-sql-server-2005-to-capture-schema-changes" rel="nofollow noreferrer">this article</a> that details how to create a changelog system for your database.</p>\n'}]
['https://Stackoverflow.com/questions/51288', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1915/']