You are here:Home » tsql » Surround With Snippets - SQL Server Denali

Surround With Snippets - SQL Server Denali

I would like to write something new and interesting feature of SQL Server 2011. I am going to introduce "Surround With" feature with small demo here.

It is new enhancement in query editor.
It is used to enclose the t-sql statements in construct with BEGIN...END,  IF...ELSE, WHILE.

Le's go through with some examples and demo.

IF ( Object_id('SurroundWithTab') > 0 )
  DROP TABLE SurroundWithTab

GO

CREATE TABLE SurroundWithTab
  (
     Id  INT,
     Val INT
  )

GO

INSERT INTO SurroundWithTab
VALUES      (1,1),
            (2,2),
            (3,3)

GO

SELECT *
FROM   SurroundWithTab 


Now select the t-sql statement which you want to enclose with constructor.
After selecting right click and press Ctrl + K, Ctrl + S.


After pressing Surround With or Ctrl + K, Ctrl + S it will appear with three code snippets as follow,


Now double click on Begin, the selected t-sql statement will construct as following,

BEGIN

SELECT *
FROM   SurroundWithTab
 
END

Double click on IF, the selected t-sql statement will construct as following,

IF( Condition )
BEGIN

SELECT *
FROM   SurroundWithTab
 

END

Double click on While, the selected t-sql statement will construct as following,

WHILE( Condition )
BEGIN

SELECT *
FROM   SurroundWithTab 

END

For more information you can refer here.