views:

26

answers:

1

I have a script to update my database for a new release of my web app . In this update i need to alter a strored procedure. I have an ALTER PROCEDURE script that works fine when run on its own, however when I drop it into my update script and run it I get the errors "Incorrect syntax near the keyword 'PROCEDURE'." and "Must declare the scalar variable "@age"." What am I doing wrong here? The script is as follows:

BEGIN TRY
BEGIN TRANSACTION

-- ==================================================================
--  v0.1 to v0.2
-- ==================================================================
IF EXISTS
(
    SELECT * FROM SystemParameters WHERE Name = 'Version' AND Value = '0.1'
)
BEGIN
    -- ==============================================================
    --  Changed Stored Procedures
    -- ==============================================================
    ALTER PROCEDURE ClearCache 
        @age int = 120
    AS
    BEGIN
        DECLARE @timestamp DATETIME
        SELECT @timestamp = DATEADD(MINUTE, -@age, GETDATE())

        --  Clear old searches

    END
    -- ==============================================================
    --  Update the Version Number
    -- ==============================================================
    UPDATE SystemParameters SET Value = '0.2' WHERE Name = 'Version'
END

COMMIT TRANSACTION
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION

--  Report the Error
DECLARE @ErrorMessage NVARCHAR(4000);
DECLARE @ErrorSeverity INT;
DECLARE @ErrorState INT;

SELECT 
    @ErrorMessage = ERROR_MESSAGE(),
    @ErrorSeverity = ERROR_SEVERITY(),
    @ErrorState = ERROR_STATE();

RAISERROR (@ErrorMessage, @ErrorSeverity, @ErrorState)

END CATCH

Any help would be appreciated :)

+1  A: 

I doubt you can alter the procedure inside the IF clause Try using it as a dynamic SQL

EXEC
(
'ALTER PROCEDURE ClearCache  
        @age int = 120 
    AS 
    BEGIN 
        DECLARE @timestamp DATETIME 
        SELECT @timestamp = DATEADD(MINUTE, -@age, GETDATE()) 

        --  Clear old searches 

    END 
'
)
Madhivanan
This has done the trick! Thanks very much
Boob
You are welcome
Madhivanan