Léster
Léster

Reputation: 1279

How to separate SQL code in blocks in SSMS?

In Oracle, I can separate blocks of PL/SQL code with the / character, and Oracle SQL Developer knows that when I run code with Ctrl+Enter, it's supposed to run all the code it finds until finding a /. Is there anything similar for SQL Server Management Studio (or T-SQL)?

Upvotes: 0

Views: 702

Answers (1)

Thom A
Thom A

Reputation: 95561

As I note in the comment, one method is to highlight the SQL you want to execute, and then press the Execute Button (or F5) and only the SQL you have selected will be run.

enter image description here

This works in other Microsoft based IDEs too, such as Azure Data Studio:

enter image description here

Note, however, that when using such techniques that only variables defined in the highlighted SQL will be accessible. Take, the following statement for example:

DECLARE @MyString nvarchar(50) = N'This is my string';

SELECT @MyString;

SELECT CONCAT(@MyString, N' again');

If you highlight the last row only, you will get an error stating that the variable @MyString has not be declared:

enter image description here

Upvotes: 1

Related Questions