I am using SqlServer for the first time, and each of our scripts for creating a procedure has a code block, as shown below, to delete a procedure if it already exists:
IF EXISTS (SELECT * FROM information_schema.routines WHERE routine_name = 'SomeProcedureName' AND routine_type = 'PROCEDURE' BEGIN DROP PROCEDURE SomeProcedureName END //then the procedure definition
To stop the cutting and paste this template code into each file, I would like to put this code in my own stored procedure so that the scripts look like this instead:
DropIfRequired('SomeProcedureName')
My attempt at a solution:
CREATE PROCEDURE DropIfRequired ( @procedureName varchar ) AS IF EXISTS (SELECT * FROM information_schema.routines WHERE routine_name = @procedureName AND routine_type = 'PROCEDURE') BEGIN DROP PROCEDURE @procedureName END
But I get the following error:
Msg 102, Level 15, State 1, DeleteProcedure Procedure, Line 10 Invalid syntax next to '@procedureName'.
Any ideas how to do what I want?
sql-server tsql
Argos
source share