Are statements after END in stored procedure executed?
I found that a stored procedure of mine includes a drop procedure after the BEGIN/END block. However, every time I execute the stored procedure elsewhere in code it seems to work fine and the
pvd_sp_yyy is not dropped. I am not sure why? I am worried about this in the first place and so I am going to remove the additional statement regardless.
Does anyone have any ideas about this?
Thanks
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER PROCEDURE [dbo].[pvd_xxx]
@var
AS
BEGIN
DECLARE @RETURN int
SET @RETURN = 0
IF EXISTS (
SELECT * FROM table1
WHERE name = @var
)
BEGIN
SET @RETURN = 1
END
RETURN @RETURN
END
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pvd_sp_yyy]') AND type in (N'P', N'PC'))
DROP PROCEDURE [pvd_sp_yyy]
BEGIN/END are not the limits of a stored procedure. The end of batch (GO usually) is.
So, yes, code is executed.
This also means the BEGIN/END are unnecessary… like parenthesis around parameters. This is SQL: not a high level client language.
I’ve quite often left a GRANT EXECUTE inside the stored proc… 🙂