sql server #region

Sql ServerRegion

Sql Server Problem Overview


Can I create regions in sql server editor (like #region and #endregion in C#) ?

Sql Server Solutions


Solution 1 - Sql Server

Not really, Sorry! But...

Adding begin and end.. with a comment on the begin creates regions which would look like this...bit of hack though!

screenshot of begin end region code

Otherwise you can only expand and collapse you just can't dictate what should be expanded and collapsed. Not without a third-party tool such as SSMS Tools Pack.

Solution 2 - Sql Server

(I am developer of SSMSBoost add-in for SSMS)

We have recently added support for this syntax into our SSMSBoost add-in.

--#region [Optional Name]
--#endregion

It has also an option to automatically "recognize" regions when opening scripts.

Solution 3 - Sql Server

BEGIN...END works, you just have to add a commented section. The easiest way to do this is to add a section name! Another route is to add a comment block. See below:

BEGIN  -- Section Name
/* 
Comment block some stuff  --end comment should be on next line
*/

 --Very long query
SELECT * FROM FOO
SELECT * FROM BAR
END

Solution 4 - Sql Server

Not out of the box in Sql Server Management Studio, but it is a feature of the very good SSMS Tools Pack

Solution 5 - Sql Server

It is just a matter of using text indentation in the query editor.

Expanded View:

Expanded

Collapsed View:

Collapsed

Solution 6 - Sql Server

No, #region does not exist in the T-SQL language.

You can get code-folding using begin-end blocks:

-- my region
begin
-- code goes here
end

I'm not sure I'd recommend using them for this unless the code cannot be acceptably refactored by other means though!

Solution 7 - Sql Server

I've used a technique similar to McVitie's, and only in stored procedures or scripts that are pretty long. I will break down certain functional portions like this:

BEGIN /** delete queries **/

DELETE FROM blah_blah

END /** delete queries **/

BEGIN /** update queries **/

UPDATE sometable SET something = 1

END /** update queries **/

This method shows up fairly nice in management studio and is really helpful in reviewing code. The collapsed piece looks sort of like:

BEGIN /** delete queries **/ ... /** delete queries **/

I actually prefer it this way because I know that my BEGIN matches with the END this way.

Solution 8 - Sql Server

Another option is

if your purpose is analyse your query, Notepad+ has useful automatic wrapper for Sql.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRaufView Question on Stackoverflow
Solution 1 - Sql ServerDog EarsView Answer on Stackoverflow
Solution 2 - Sql ServerAndrei RantsevichView Answer on Stackoverflow
Solution 3 - Sql ServerBClaydonView Answer on Stackoverflow
Solution 4 - Sql ServerPero P.View Answer on Stackoverflow
Solution 5 - Sql ServerBharath theorareView Answer on Stackoverflow
Solution 6 - Sql ServerMattView Answer on Stackoverflow
Solution 7 - Sql ServerandylizeView Answer on Stackoverflow
Solution 8 - Sql ServerTekin GüllüView Answer on Stackoverflow