September 18, 2007 at 3:15 am
When writing a stored proc is it best practise to check to check if the proc exists and dropping and recreating it if it does?
The question I have surrounding this is if I am dropping a proc and recreating it does that not force the query engine to recreate an execution plan each time and therefore losing the performance benefits of utilising stored procedures?
Thanks
September 18, 2007 at 4:04 am
I'm a bit confused... Are you writing some stored procedure that modifies another stored procedure and then runs it?
Or are you asking whether you should drop and recreate a procedure every time you want to run it?
If you're talking about development added to production, then I would recommend check/drop/create. But this only happens now and then, so you have no reason to be concerned about recreating the execution plan... Could you be more specific, please?
September 18, 2007 at 4:26 am
Interesting question I think.
While I see loads of scripts which first check "if exists then drop" I usually argue it's better to use ALTER in case an objects already exists, because you won't have to grant all the permissions again.
So my "best practise" would be NO, don't drop and recreate.
The point about "recreating execution plans" makes no sense because a new execution plan will be needed in both cases.
Markus
[font="Verdana"]Markus Bohse[/font]
September 18, 2007 at 5:21 am
Well, it probably depends on environment. If you write a script that will apply changes in a procedure to several databases (like when several clients use "copies" of the same database - meaning structure copies, not the same data), and want to use ALTER, it means you have to script it out twice. Once as ALTER and once as CREATE (in case the procedure does not exist for some reason). If you use DROP/CREATE, you only script the CREATE, but have to script permissions as well. I think it is more a matter of personal preference...
Of course, there is also a possibility to script just ALTER and throw error "Procedure missing!" if it does not exist, and then deal with it separately.
BTW, you can't see when the procedure was last altered, but you can see "date created". So, for possible future searches when precisely it was last modified, DROP/CREATE gives you the info you need.
I agree that the piece about execution plans is confusing, I didn't understand that either.
September 18, 2007 at 5:48 am
Execution plan may be changed even if nobody was touching SP for years.
It depends in statistics, on indexes, etc., not only on SP code.
If after next data upload statistics will be changed dramatically optimiser may decide that existing plan is not most effective any more and work out new one.
And keeping SP unchanged won't help here.
I use to say that optimising query performance starts with CREATE TABLE statement.
Code is important as well, but not always defining.
_____________
Code for TallyGenerator
September 18, 2007 at 7:41 am
>> BTW, you can't see when the procedure was last altered, but you can see "date created". So, for possible future searches when precisely it was last modified, DROP/CREATE gives you the info you need.
Although true in SQL Server 2000, in SQL Server 2005 you can track when objects are modified.
September 18, 2007 at 2:38 pm
Always alter - if the procedure does not exist, then create a "stub" that does nothing and then alter. If you drop/create then any security privileges will need to be included in the scriot and object dependency will get lost. Specifically, what other stored procdures execute this procedure.
IF OBJECT_ID('dbo.myprocedure') is null
create procedure dbo.myprocedure
as
return 0
end
alter procedure dbo.myprocedure
as
-- the real source
go
SQL = Scarcely Qualifies as a Language
September 18, 2007 at 5:40 pm
My recommendation in SQL Server 2000 would be to always drop the proc so that you can easily tell, just by the CreateDate, that you have the correct rev especially in the absence of all other documentation (usually the case, no documentation). And, if you have Query Analyzer create the code from production before you alter it and Query Analyzer is setup for it, it will automatically generate the existing Grants.
On SQL Server 2005, shouldn't make a difference. But, I can see where folks might prefer alter over drop. Either way, not likely you'll break any code.
--Jeff Moden
Change is inevitable... Change for the better is not.
September 19, 2007 at 7:24 am
Oracle has the "CREATE OR REPLACE" syntax which I really liked.
Lacking that in SQL Server, I create each stored procedure or function as a stub. Then the main procedure or function file (in our source control system) contains an "ALTER" statement.
We "build" the main schema creation DDL script by taking multiple individual source files (from the source control system) and concatenate them together according to a "build" recipe. Having the file contain an "ALTER" means that we can use the same source file for both new schema builds as well as for upgrades to schemas in existing databases. I.e., you don't have to code it twice, test for existance, worry about grants and other issues, etc. As for modification timestamp, SQL Server 2005 has the "LAST_ALTERED" column in the information_schema.routines view.
Viewing 9 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy