473,385 Members | 1,661 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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?
Jul 25 '08 #1
2 8203
On Fri, 25 Jul 2008 08:03:24 -0700 (PDT), "semaj.remle 'at' gmail"
<se*********@gmail.comwrote:
>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
Jul 25 '08 #2
semaj.remle 'at' gmail (se*********@gmail.com) writes:
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, es****@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
Jul 25 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: M Simpson | last post by:
/* for the google index */ ALTER TABLE DEFAULT COLUMN DEFAULT VALUE I've worked out several stored procedures for altering the default column values in a table. They were compiled from books...
2
by: me | last post by:
I would like to add an Identity to an existing column in a table using a stored procedure then add records to the table and then remove the identity after the records have been added or something...
7
by: pb648174 | last post by:
I am trying to add a column to a temp table and then immeditaely query against that new column. If I do this in Query Analyzer it works fine as long as there is a go in between, but I can't use a...
10
by: BuddhaBuddy | last post by:
Platform is DB2/NT 7.2.9 The table was created like this: CREATE TABLE MYTEST ( MYTESTOID bigint not null primary key, FK_OTHEROID bigint not null references other, FK_ANOTHEROID bigint not...
3
by: CK | last post by:
Hi All, Quick question, I have always heard it best practice to check for exist, if so, drop, then create the proc. I just wanted to know why that's a best practice. I am trying to put that theory...
7
by: Serge Rielau | last post by:
Hi all, Following Ian's passionate postings on problems with ALTOBJ and the alter table wizard in the control center I'll try to explain how to use ALTOBJ with this thread. I'm not going to get...
11
by: raylopez99 | last post by:
Keep in mind this is my first compiled SQL program Stored Procedure (SP), copied from a book by Frasier Visual C++.NET in Visual Studio 2005 (Chap12). So far, so theory, except for one bug...
2
by: devlin | last post by:
HI. I have to Alter a procedure if it exists or Create the procedure if it doesn't in SQL Server 2005. I can manage to determine if it exists or not, but when i have the code to create and alter...
0
by: Eric Isaacs | last post by:
Optional Create and Alter is nice because it fails, the original is still in place as are the original permissions. Drop and create is also valid in some cases if you want to clear/reset the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.