Syntax error in the ALTER PROCEDURE program in updating the database script

I have a script to update my database for a new version of my web application. In this update, I need to modify the stored procedure. I have an ALTER PROCEDURE script program that works fine when run by itself, however, when I add it to my script update and run it, I get errors "Incorrect syntax next to the keyword" PROCEDURE "." and "Must declare scalar variable" @age "". What am I doing wrong here? The script looks like this:

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 :)

+3
source share
1

, IF SQL

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

        --  Clear old searches 

    END 
'
)
+4

Source: https://habr.com/ru/post/1753122/


All Articles