Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Detecting ALTER or CREATE PROCEDURE?? Expand / Collapse
Author
Message
Posted Sunday, July 28, 2013 7:38 PM


Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, April 01, 2014 1:16 AM
Points: 20, Visits: 105
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>
Post #1478429
Posted Sunday, July 28, 2013 10:28 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 2:24 AM
Points: 3,591, Visits: 5,100
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!
Post #1478438
Posted Sunday, July 28, 2013 10:42 PM


Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, April 01, 2014 1:16 AM
Points: 20, Visits: 105
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
Post #1478440
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse