views:

365

answers:

1

In MS SQL Server Management Studio 2005:

If you have the setting Tools|Options|Scripting|"Include IF NOT EXISTS clause" set to true, modifying a stored procedure will create a strange script that doesn't even work. It looks something like the following: (ellipsis used for brevity)

 SET QUOTED_IDENTIFIER ON
 GO
 IF NOT EXISTS [...]
 BEGIN
 EXEC dbo.sp_executesql @statement = N'
 ALTER procedure [dbo].[p_Procedure]
 [...]
 '
 END

This obviously doesn't work because the only way the ALTER statement is called, is if the stored procedure DOESN'T exist.

The question is thus: is there any way of changing this generated code? A template out there somewhere (this doesn't seem to be related to the build in Templating tools)?

(A slight explanation for this behavior: scripting a CREATE statement generates the same code in which the IF NOT EXISTS makes more sense)

A: 

There are some issues on this topic on MS-feedback site. Here are one:

https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=260519

Here are one comment on that issue (from the bottom of the page linked above):

In SQL2000, the approach was If exists, DROP followed by a CREATE. This worekd flawlessly and covered all cases. It wored so well that we built our deployment off of this scripting model. Since SQl2005 entered our world, we have had manual, cumbersome workarounds to replace the automated scripting that was lost in the move to SQL2000.

Please do readd the If exists, DROP followed by a CREATE approac. It was great the way

Stefan

related questions