Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Is there a way to create collapsable regions in the SQL script file like we create in VS using #region ..... #endregion ?

--EDIT--

Script file contains DDL statements like Alter Table and Alter View etc. and i want to group them in regions like "Table Related Statements" "View Related Statements" etc.

like image 350
Asdfg Avatar asked Aug 15 '11 14:08

Asdfg


People also ask

How do I create a SQL Server database?

Use SQL Server Management StudioRight-click Databases, and then select New Database. In New Database, enter a database name. To create the database by accepting all default values, select OK; otherwise, continue with the following optional steps. To change the owner name, select (...) to select another owner.


2 Answers

Yes, with SSMS Tools Pack

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 198
gbn Avatar answered Sep 23 '22 15:09

gbn


No, but you can simulate it in SSMS by using begin..end blocks as follows:

--Region 1
begin
  --Do Something
end

--Region 2
begin 
  --Do Something Else
end

The begin..end pair is collapsible.

EDIT:

This only works for DML scripts. DDL scripts are more picky as many statements require to be the first in the batch (original question did not state DDL use.)

I don't think there's a way to do this within one script file for DDL - you're into breaking down the script into smaller scripts or stored procs.

like image 40
Jon Egerton Avatar answered Sep 25 '22 15:09

Jon Egerton