SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Detecting ALTER or CREATE PROCEDURE??


Detecting ALTER or CREATE PROCEDURE??

Author
Message
Tergum Rufus
Tergum Rufus
SSC-Enthusiastic
SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)

Group: General Forum Members
Points: 193 Visits: 108
Can't get this working - whinges about ALTER/CREATE statement syntax ?? Help?

IF (SELECT count(name) FROM sys.sysobjects WHERE (type = 'P') and name = 'SPNAME') > 0
BEGIN
ALTER PROCEDURE [spname] ----
END
ELSE BEGIN
CREATE PROCEDURE [spname] ----
END
AS
<body of the sp>
dwain.c
dwain.c
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18139 Visits: 6431
Grenouillaise (7/28/2013)
Can't get this working - whinges about ALTER/CREATE statement syntax ?? Help?

IF (SELECT count(name) FROM sys.sysobjects WHERE (type = 'P') and name = 'SPNAME') > 0
BEGIN
ALTER PROCEDURE [spname] ----
END
ELSE BEGIN
CREATE PROCEDURE [spname] ----
END
AS
<body of the sp>


I believe the CREATE or ALTER PROCEDURE needs to be the first SQL in a batch.

Try something like this:


IF (SELECT count(name) FROM sys.sysobjects WHERE (type = 'P') and name = 'SPNAME') > 0
DROP PROCEDURE [spname] ----
GO
CREATE PROCEDURE [spname] ----
AS




My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Tergum Rufus
Tergum Rufus
SSC-Enthusiastic
SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)

Group: General Forum Members
Points: 193 Visits: 108
Thank you Dwain,
True what you say.
I was trying not to use DROP per various blogs herein. Since posting I have done it this way.
USE <dbname>
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
declare @S VARCHAR (16), @A varchar (8), @B varchar (MAX)
SET @S='SPname'
if object_id(@S,'P') is not null
begin
set @A='ALTER'
end
else begin
set @A='CREATE'
end

set @B = ' PROCEDURE [dbo].[' + @S + ']
<procedure script>'
SET @B=@A+@B
EXEC @B
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search