Rauf
Rauf

Reputation: 12852

sql server #region

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

Upvotes: 203

Views: 193418

Answers (10)

nimblebit
nimblebit

Reputation: 559

If you don't want to add a plugin to SSMS you can do it like this and get the collapsible and expandable code blocks

BEGIN --#region [DisplayDate]
    
    SELECT GETDATE() CurrentDate

END   --#endregion

Using regions in SQL code sections

Using regions in SQL code sections - collapsed section

You don't have to add #region and #endregion and can just specify the description in it's place as others have suggested previously.

Credit: https://stackoverflow.com/a/22633580/1165173

Upvotes: 1

Emel KIRIMLI
Emel KIRIMLI

Reputation: 14

begin --do something end GO

begin --do something else end GO

Upvotes: -1

Bharath theorare
Bharath theorare

Reputation: 554

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

Expanded View:

Expanded

Collapsed View:

Collapsed

Upvotes: 9

Dog Ears
Dog Ears

Reputation: 10025

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.

Upvotes: 344

Tekin Güllü
Tekin Güllü

Reputation: 363

Another option is

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

Upvotes: 2

BClaydon
BClaydon

Reputation: 1970

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

Upvotes: 12

Pero P.
Pero P.

Reputation: 26992

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

Upvotes: 7

Andrei Rantsevich
Andrei Rantsevich

Reputation: 2945

(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.

Upvotes: 27

andylize
andylize

Reputation: 41

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.

Upvotes: 4

Matt
Matt

Reputation: 498

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!

Upvotes: 4

Related Questions