Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there way to create regions in .sql scripts?

Tags:

sql-server

I am filling in lots of predefined data into a table (in my post-deployment sql script) Is there way to create C# style regions so I can collapse chunks of inserts for readability and sanity?

Here's a short sample (#region is commented out):

MERGE INTO [MYTable] AS Target
USING (VALUES
        --#region Measurement_Id = 1 'Flow' for all reasources
         ( 1,  1, 1, 45, NULL, NULL)
        ,( 2,  2, 1, 56, NULL, NULL)
        ,( 3,  3, 1, NULL, NULL, NULL)
        ,( 4,  4, 1, 67.5, 87, NULL)
        ,( 5,  5, 1, NULL, NULL, NULL)
        ,( 6,  6, 1, 54., NULL, 87)
        ,( 7,  7, 1, NULL, NULL, NULL)
        --#endregion

        --#region Measurement_Id = 2 'Disc' for all reasources 
         ( 1,  1, 2, NULL, NULL, NULL)
        ,( 2,  2, 2, NULL, NULL, NULL)
        ,( 3,  3, 2, NULL, NULL, NULL)
        ,( 4,  4, 2, NULL, NULL, NULL)
        ,( 5,  5, 2, NULL, NULL, NULL)
        --#region
                ...

         ) AS Source ([Measurement_Id],[Measurement_Nm],[Measurement_Cd],[Measurement_Desc],[IsActive_Ind]) 
        ON (Target.[Measurement_Id] = Source.[Measurement_Id])
like image 353
denis morozov Avatar asked Feb 10 '26 03:02

denis morozov


2 Answers

There is no native support, but it is possible to realize it using an add-in.

I develop SSMSBoost add-in (www.ssmsboost.com) for SSMS and have added

--#region [name]

--#endregion

syntax support in last version (2.12). There is also an option to auto-parse opened files, so that regions will be displayed immediately.

like image 156
Andrei Rantsevich Avatar answered Feb 13 '26 13:02

Andrei Rantsevich


Database - how to create regions in TSQL (SQL Server 2008)

"Regions and Debug sections

Regions behave in the same way as in Visual Studio. You can collapse them and expand them. Debug sections are sections that get commented when you change your script to Release configuration. A debug section is also a collapsable region. If you deploy a script in debug mode with added debug sections it will fail when run from SSMS without SSMS Tools Pack installed. You can of course comment those sections yourself by simply searching for start and end text of the debug sections".

like image 26
user3241191 Avatar answered Feb 13 '26 14:02

user3241191



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!