Detecting ALTER or CREATE PROCEDURE??

  • 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>

  • 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![/I]

    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?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • 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-2 VARCHAR (16), @a varchar (8), @b-2 varchar (MAX)

    SET @s-2='SPname'

    if object_id(@S,'P') is not null

    begin

    set @a='ALTER'

    end

    else begin

    set @a='CREATE'

    end

    set @b-2 = ' PROCEDURE [dbo].[' + @s-2 + ']

    <procedure script>'

    SET @b-2=@A+@B

    EXEC @b-2

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply