in

ASP.NET Weblogs

This Blog

Syndication

Tags

Archives

BenL's WebLog

Maintaining SQL Server Databases

I read an interesting article by Eli Robillard recently.  The very top of the article quotes Hlade's Law, "If you have a difficult task, give it to a lazy person - they will find an easier way to do it."  Ambrose points out the benefits of lazy people in military positions in the comments.  I enjoyed reading these things because it made me realize that I approach my work in a similar way.  But I digress...

In keeping with the spirit of “The Lazy Programmer” I am going to briefly describe a method a friend of mine showed me for maintaining SQL Server databases.  I will not outline the details of the approach.  Instead, I will provide a brief overview and reserve any details for a (possible) future blog article.

The method my friend showed me involves setting up a main folder for your database with various sub folders to hold the different pieces.  For example, you might have a root folder called “Northwind” with sub folders called “SP” (for stored procedures), “Table” (for tables), “PK” for primary keys, “FN” (for user-defined functions), etc.

The contents of all of these folders contain script files to create each item.  Sticking with the North wind example, the “Table” folder would contain files called “Customers.sql”, “Orders.sql”, “Employees.sql”, etc.  The “PK“ folder would have script files for creating primary keys on the tables in the database.  Obviously, databases with lots of objects would have many script files in using this approach.

After all the .sql files are created there are a couple of batch files that you run to extract all of the individual scripts into one, large script file that can be run in Query Analyzer to drop and rebuild the entire database.  These batch files can easily be run from other files or processes (say a giant application build script).

It took some getting used to, but I have found working directly with the script files (to create tables, write sproc's, create indexes, etc.) to be much more efficient than doing things in Enterprise Manager.  For example, after creating the script to create a table, copying the column names and data types into another script file for an Insert stored procedure is very easy.  Also, using the regular expression capabilities of a tool like Visual Studio.Net really speeds up development time.

I would be more than happy to share more about my experiences with this method.  What do you find useful in creating/maintaining SQL Server databases?

Published Jan 14 2004, 06:40 PM by BenL
Filed under:

Comments

 

Justin said:

In Query Analyzer, if you have the Object Browser open, you can right click and have scripts generated for you too. Sounds very similiar but the QA method saves you the time of having to script the DDL yourself.
January 14, 2004 10:23 PM
 

John Bates said:

Very good idea; I use a similar method. The best benefit for me is that each SP, etc. is version controlled and can have a SCC-generated comment with the version of that script. Really handy when debugging a problem at a customer site - just check version numbers to see if they're running the latest scripts.
January 14, 2004 11:35 PM
 

Jerry Pisk said:

I use a similar approach as well, except we use prefixes instead of fodlers so you can tell what objects you're working with (so our user stored procedure are all named up_module_subject_action). And we keep primary keys, indexes, foreign keys and constraints together with table scripts as the tables should never be created without those (it's just too error prone if you have to remember to manually create a primary key after you create a table).

The side effect of using script is that it keeps less experienced developers (read people who code using their mouse) out of the database ;)
January 15, 2004 2:27 AM
 

Ben Lower said:

Justin:
I am aware of the scripting feature. It is very nice. However, I've found that by starting with the script from the very beginning (not creating tables via Ent. Mgr.), I can really save time by copying the DDL code into my SPROC scripts.

Jerry:
Your comments reminded me of a nice MSDN article from 4/2003 that deals with automated creation of SQL code & using prefixes. (http://msdn.microsoft.com/msdnmag/issues/03/04/storedprocedures/default.aspx)
January 15, 2004 11:50 AM
 

TrackBack said:

^_^,Pretty Good!
April 10, 2005 5:36 AM

Leave a Comment

(required)  
(optional)
(required)  
Add