Collapsed Regions using BEGIN_END

#0409 – SQL Server – Code Blocks – Equivalent of #region…#endregion


I was recently participating in a forum and came across an interesting question. What attracted my attention was that the person was trying to keep their T-SQL code clean and readable (which in itself is a rare sight).

The person was trying to group their T-SQL code into regions. In the world of application development technologies (e.g. C#) we would typically use the #region….#endregion combination. However, it does not work with T-SQL because the hash (#) is used to define temporary tables.

In T-SQL, the basic control-of-flow statements  that allow you to group the code are the BEGIN…END keywords. The BEGIN…END keywords can be used to logically group code so that they can be collapsed or expanded as required.

Collapsed Regions using BEGIN_END

Collapsed Regions using BEGIN_END

Expanded Regions using BEGIN_END

Expanded Regions using BEGIN_END

Summarizing,

The BEGIN…END keywords are therefore the functional equivalents of the #region…#endregion statements.

Until we meet next time,

Be courteous. Drive responsibly.

2 thoughts on “#0409 – SQL Server – Code Blocks – Equivalent of #region…#endregion

  1. Pingback: #0409 – SQL Server – Code Blocks – Equivalent of #region…#endregion - SSWUG.ORG

  2. Miroslav Makhruk's avatarMiroslav Makhruk

    The problem is that you can’t put BEGIN and END inside some SELECT query with a long list of selected columns. E.g. you need to concentrate on the FROM and WHERE parts of the query so you need to fold only the SELECT part and that you cannot achieve using BEGIN and END…

    Like

    Reply

Let me know what you think about this post by leaving your feedback here!

This site uses Akismet to reduce spam. Learn how your comment data is processed.