To ALTER or to DROP/CREATE... That is the Question

Whether 'tis nobler in the CPU to suffer the slings and arrows of outraged users... oh, never mind.  Back to the topic at hand which really is all about how you modify your SQL Server objects.  Specifically, I'm going to talk about Views and Stored Procedures (a.k.a. sprocs).  Tables are a completely different creature in regard to this topic.

It will come as no surprise to my readers that the first thing I'm going to tell you is to write a script to make the changes.  Do NOT go to Enterprise Manager to make changes to a sproc!  Just don't do it!  It's a bad habit to get into, and once you've made the change and tested it in your development system (you DO test object changes in your development system before applying them to QA and production, don't you) then you'll need to make the same changes in your other systems and you never really know if you made exactly the same changes in each of the systems if you're doing it manually.

OK, so I've convinced you to write a script to make your changes.  Great.  Now back to the original topic... do you ALTER your sproc/view or do you DROP it and then CREATE it with the new definition?  I prefer to DROP/CREATE, and here are a few reasons why:

  • I do a check for existence before I drop, and can therefore use the same script in a brand new database as I did in my development database.
  • SQL Server does not keep any stats on when an object was last updated or altered, but it does keep the date it was created.  So if I ever care, it's there.
  • It just feels cleaner to me.  I like the idea that I can grab the latest version, run it, and BAM! we're ready to go.
One drawback to this is that you have to reassign all the permissions for this procedure.  With ALTER, the permissions remain intact.  But this has not really been an issue for me because I save the GRANT statements as part of my script anyway (hey, what do you think the GO statement is for?)

posted @ Saturday, January 24, 2004 10:56 PM

Print

Comments on this entry:

# re: To ALTER or to DROP/CREATE... That is the Question

Left by Tara at 1/26/2004 4:01 PM
Gravatar
I typically use ALTER in development environment. I use DROP/CREATE method in all other environments. DROP/CREATE is so much cleaner for QA and production environments since your script could get an error on ALTER if it doesn't exist. For development, it really is just a preference thing.

# re: To ALTER or to DROP/CREATE... That is the Question

Left by Lavos at 2/3/2004 9:49 AM
Gravatar
Are you sure about keeping track of created/last altered?

I seem to remember INFORMATION_SCHEMA.ROUTINES had a last_altered column in addition to created. I used to use them a good deal for finding new procs.

I can't find a similar property for views, so I guess your point still stands :)

Personally, I think the drop/create is better, but in development I tend to not bother and just right-click and edit from QA, and then generate the scripts later.

# re: To ALTER or to DROP/CREATE... That is the Question

Left by Kristen at 4/12/2004 4:44 AM
Gravatar
We use IF EXISTS ... DROP and then a CREATE (as well as putting GRANT permssions stuff at the end of the script).

However, this tends to muck up the DEPENDS and we are thinking of changing to
IF NOT EXISTS ... CREATE dummy SP followed by an ALTER and the PERMISSIONS etc.

The CRDATE in sysobjects is handy, but we have an EXEC xx_SP_LogMyScript('MyScriptName', 'MyVersionNo') at the top of all our scripts that kinda soes the same thing (and gives a history and the ability to compare version numbers between database instances
Comments have been closed on this topic.
«October»
SunMonTueWedThuFriSat
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678