BEGIN and END in a SQL Server Stored Procedure

Bobby P 231 Reputation points
2022-06-17T18:07:34.407+00:00

What are the general standard/convention regarding using BEGIN and END in a SQL Server Stored Procedure?

I totally understand using BEGIN and END for complex SQL Server Stored Procedures to separate logical chunks of code, but should they necessarily be used for simple SQL Server Stored Procedures for like readability?

Would love to hear people's opinions on using BEGIN and END in SQL Server Stored Procedures.

Thanks for your review and am hopeful for some solid, good, quality replies.

Developer technologies Transact-SQL
SQL Server Other
0 comments No comments
{count} votes

5 answers

Sort by: Most helpful
  1. Tom Phillips 17,771 Reputation points
    2022-06-17T18:54:53.563+00:00

    They are completely optional and only used for "style". I don't use them unless required after IF/WHILE blocks.

    0 comments No comments

  2. Naomi Nosonovsky 8,431 Reputation points
    2022-06-17T20:10:38.073+00:00

    Red-Gate SQL Prompt suggests adding them at the very top of the procedure and the end, so I always have

    create or alter storedProcName

    parameters
    /*
    Comment block

    */
    AS
    BEGIN

     SET NOCOUNT, XACT_ABORT ON;  
    
     declare...  
    
     begin try  
     end try  
     begin catch  
     end catch  
    

    END
    GO

    0 comments No comments

  3. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2022-06-17T21:36:31.97+00:00

    As Tom says they are optional. Quite a few people add BEGIN-END for the entire procedure code:

       CREATE PROCEDURE ... AS  
       BEGIN   
          ...  
       END  
    

    Being an old-timer with SQL I'm not of that school. Then again, about any piece of production code I write goes like this:

       CREATE PROCEDURE ... AS  
       BEGIN TRY  
          ...  
       END TRY  
       BEGIN CATCH  
         ...  
       END CATCH  
    

    As for a piece of code inside a stored procedure, I tend to write like this:

       IF @x > 2  
           SELECT @x = 2  
    

    I don't use BEGIN-END here. However, if it is a long SELECT statement I do:

       IF @x > 2  
       BEGIN   
           SELECT ...  
           FROM   ...  
           JOIN   ...  
           LEFT JOIN ...  
           WHERE ...  
           GROUP BY   
       END  
    

    But this is certainly a matter of preference.

    0 comments No comments

  4. LiHong-MSFT 10,056 Reputation points
    2022-06-20T02:58:07.683+00:00

    Hi @Bobby P
    The BEGIN... END statement bounds a logical block of SQL statements. I often use the BEGIN...END at the start and end of a stored procedure and function. But it is not strictly necessary.

    However, the BEGIN...END is required for the IF ELSE statements, WHILE statements, etc., where you need to wrap multiple statements.
    Sometimes, the statement block can be nested. It simply means that you can place a BEGIN...END statement within another BEGIN... END statement. Like this:

    BEGIN  
        DECLARE @name VARCHAR(MAX);  
      
        SELECT TOP 1 @name = product_name  
        FROM production.products  
        ORDER BY  list_price DESC;  
          
        IF @@ROWCOUNT <> 0  
        BEGIN  
            PRINT 'The most expensive product is ' + @name  
        END  
        ELSE  
        BEGIN  
            PRINT 'No product found';  
        END;  
    END  
    

    For more details, please refer to this article: SQL Server BEGIN END

    Best regards,
    LiHong

    0 comments No comments

  5. Bobby P 231 Reputation points
    2022-06-22T18:29:00.623+00:00

    So just to clarify...the BEGIN and END will NOT Transactionalize the sequence...And what I mean by that if we have multiple DELETEs within a BEGIN and an END, if one of the DELETEs fails for whatever reason, it is NOT going to roll back the previous DELETEs that may have already run then?


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.