Connecting Tech Pros Worldwide Help | Site Map

ALTER proc vs IF EXISTS DROP/CREATE

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 25th, 2008, 03:05 PM
semaj.remle 'at' gmail
Guest
 
Posts: n/a
Default ALTER proc vs IF EXISTS DROP/CREATE

For files saved in source control, is it better to use code to DROP/
CREATE a procedure like this:
------------------------------------------------------------------
IF OBJECT_ID ('procName') IS NOT NULL
DROP PROC procName
GO
CREATE PROCEDURE procName
AS
BEGIN
--proc code

END

GO
GRANT EXECUTE ON procName TO role
GO
------------------------------------------------------------------
or is it better to use the opposite logic and create a stub proc (if
it doesn't exist) then run an alter statement like this:

------------------------------------------------------------------
IF (OBJECT_ID('procName') IS NULL)
EXEC('CREATE PROC [dbo].[procName] select ''stub''')
GO
ALTER PROC procName as select 'Proc Code'
GO
------------------------------------------------------------------

Is there any advantage to one way over the other?

  #2  
Old July 25th, 2008, 03:45 PM
Roy Harvey (SQL Server MVP)
Guest
 
Posts: n/a
Default Re: ALTER proc vs IF EXISTS DROP/CREATE

On Fri, 25 Jul 2008 08:03:24 -0700 (PDT), "semaj.remle 'at' gmail"
<semaj.remle@gmail.comwrote:
Quote:
>For files saved in source control, is it better to use code to DROP/
>CREATE a procedure like this:
>------------------------------------------------------------------
>or is it better to use the opposite logic and create a stub proc (if
>it doesn't exist) then run an alter statement like this:
It depends on how you manage your source code. The strong advantage
of DROP/CREATE/GRANT is that it works best when stored in a source
control library, but of course that requires a very disciplined
approach. If anyone does a GRANT without updating the script that
will be lost the next time someone executes the out of date script.

On the other hand if ALTER fails for some reason the previous version
still exists. If CREATE fails, the procedure was already dropped and
you could find yourself scrambling to find a copy of the old version
to put back until the problem with the new once can be fixed.

Roy Harvey
Beacon Falls, CT
  #3  
Old July 25th, 2008, 11:05 PM
Erland Sommarskog
Guest
 
Posts: n/a
Default Re: ALTER proc vs IF EXISTS DROP/CREATE

semaj.remle 'at' gmail (semaj.remle@gmail.com) writes:
Quote:
For files saved in source control, is it better to use code to DROP/
CREATE a procedure like this:
------------------------------------------------------------------
IF OBJECT_ID ('procName') IS NOT NULL
DROP PROC procName
GO
CREATE PROCEDURE procName
AS
BEGIN
--proc code
>
END
>
GO
GRANT EXECUTE ON procName TO role
GO
------------------------------------------------------------------
or is it better to use the opposite logic and create a stub proc (if
it doesn't exist) then run an alter statement like this:
>
------------------------------------------------------------------
IF (OBJECT_ID('procName') IS NULL)
EXEC('CREATE PROC [dbo].[procName] select ''stub''')
GO
ALTER PROC procName as select 'Proc Code'
GO
------------------------------------------------------------------
>
Is there any advantage to one way over the other?
I certainly advocate the latter for at least two reasons:

1) You can manage permissons seperately from managing the source
code itself. It's very difficult to manage permissions if you
them scattered all over the source code - not the least if you
need to change them.

2) DROP means that all dependicies are kissed bye-bye, so that you
no longer knows which other SPs that call this one.

As Roy points out, the latter also means that if the new script
fails, that the old version is retained. Whether this is a good
thing or not depends on where it happens, but in a development
environment it's likely to be a good thing.

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.