Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are statements after END in stored procedure executed?

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]
like image 303
bobbo Avatar asked Jan 19 '23 04:01

bobbo


2 Answers

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

like image 141
gbn Avatar answered Jan 30 '23 19:01

gbn


Yes, as all you have declared is a code block. The procedure contents do not end until you put a GO to execute the batch.

like image 25
dnolan Avatar answered Jan 30 '23 19:01

dnolan