Best way to run separate parts of a single T-SQL script together at once

powershell script to run sql query on multiple servers
run sql script against multiple databases
run sql script on multiple servers
sql server query multiple databases on different servers
run sql script from batch file sql server
sqlcmd
selecting data from two different servers in sql server
connect to multiple sql server instances

I have a some long scripts that have variables at the very top. When I want to run only part of the script with a variable included, what is the best way to do this...are there other options besides:

  1. Copy/pasting the declare variables (but this requires me to comment them in/out all the time)
  2. Just changing the variable to be hard-coded (but I'm concerned that I'll forget to change them back)

So, is there a way to highlight separately the parts I want to run...or maybe something else entirely?

This question is for SQL Server 2008

All the other answers are fine if you can/want to make functional modifications to your script, but if you would rather avoid that, as I suspect, the way I usually do what you are talking about is by block-commenting the parts I want to skip:

So I just change this:

DECLARE @variables;

Some Code I want to skip this time;

Some Code I want to execute this time;

Maybe some more code I want to skip this time;

To this:

DECLARE @variables;
/*
Some Code I want to skip this time;
*/
Some Code I want to execute this time;
/*
Maybe some more code I want to skip this time;
*/

Execute SQL Server query on multiple servers at the same time, Best way to run separate parts of a single T-SQL script together at once Unicorn Meta Zoo #1: Multi tool use. Can I criticise the more senior� 2 Best way to run separate parts of a single T-SQL script together at once Mar 7 '19. 1 Extending end date then compare repeatedly Nov 15 '18. 0 Looping updates

Sometimes I wrap portions of the code inside BEGIN...END blocks so that I could expand and collapse them inside SSMS. Turns out that you can prefix a block with an IF 0 = 1 so that it does not execute. Expanding on this idea, you could do this:

-- variables
DECLARE @p1 INT = 123;
DECLARE @p2 INT = 456;

-- which block to run
DECLARE @block AS INT = 1;

IF @block = 1
BEGIN
    SELECT 'statement block 1'
END

IF @block = 2
BEGIN
    SELECT 'statement block 2'
END

Using SQLCMD to Execute Multiple SQL Server Scripts, One of the big challenges is having to collect data from each of the And best of all, once it is setup you can even use it on SQL 2000 and SQL 2005 servers. How to setup and use a SQL Server alias), but I wouldn't recommend to all desktops by using a script, but again I wouldn't recommend using it� The easiest way to do this is to use code of some type. T-SQL isn’t going to work because we need these to run asynchronusly and not synchronously (all at the same time not one after the other) and T-SQL doesn’t have that option. You could create a bunch of agent jobs but again, huge pain when you get past a small number of queries at once.

I tend to use a special variable that I call @debug, but you can call it what you want. When I'm debugging, I'll use it to set variables, raise status messages (disguised as error messages), and that sort of thing. Then I only need to remember to reset that one value when I'm finished.

DECLARE 
  @var1 INT,
  @var2 VARCHAR(25),
  @var3 BIGINT,
  @debug TINYINT,   --Indicator that I'm in debug mode
  @msg VARCHAR(50); --Custom message text placeholder

SET @debug = 1;

IF @debug = 1
  BEGIN
    SELECT
      @var1 = 1,
      @var2 = 'Hello World',
      @var3 = 12;
  END;

SELECT @var1, @var2, @var3;

IF @debug = 1
  BEGIN
    SET @msg = 'Step 1 Complete'
    RAISERROR(@msg, 1,0) WITH NOWAIT
  END;

Top 10 SQL Server Management Studio (SSMS) Tips and Tricks , One script builds the database, another builds the tables, etc. Is it possible to concatenate these scripts together and execute them with a single call to SQLCMD? How is it that SQLCMD knows to execute CREATE_TABLES.sql before I create a DB as part of unit test setup and instead of calling� We have this requirement where we want to execute multiple .sql files one after the other. These scripts file names keep changing. So calling them individually by name wouldn't help.

Use conditionals. See @RunOne and @RunTwo for explanation.

DECLARE @MessageText as varchar(28) = 'This is not a greeting'

Declare @RunOne as bit = 1 -- set to 0 to skip first part
Declare @RunTwo as bit = 1 -- set to 0 to skip second part

IF @RunOne = 1
BEGIN
   select @MessageText + ', or is it?'
END

IF @RunTwo = 1
BEGIN
   select @MessageText + ' for wide dissemination.'
END

Connecting to multiple servers in a Query Window , I've not included any T-SQL features as this article is specifically about SSMS features. One Click Feedback that I've picked up along the way with SQL Server Management Studio. For instance the following SQL can be run in SSMS : objects and script them up all together in a single query window. Run the script file. Open a command prompt window. In the Command Prompt window, type: sqlcmd -S myServer\instanceName -i C:\myScript.sql. Press ENTER. A list of Adventure Works employee names and addresses is written to the command prompt window. Save the output to a text file. Open a command prompt window.

Just building on Salman's answer (+1).

This twist will allow for a block range rather than just one at a time.

Example

-- Declare Your Variables
Declare @V1 int = 8;
Declare @V2 int = 12;

-- Define which Blocks to Execute  
Declare @BlockRange1 int = 3
Declare @BlockRange2 int = 4


If 1 between @BlockRange1 and @BlockRange2
Begin
    Select 1 
End

If 2 between @BlockRange1 and @BlockRange2
Begin
    Select 2
End

If 3 between @BlockRange1 and @BlockRange2
Begin
    Select 3
End

If 4 between @BlockRange1 and @BlockRange2
Begin
    Select 4
End

10 SSMS Tips and Tricks to boost your Productivity, One particularly useful feature is the ability to switch between different SQL Servers Without SQLCMD the usual way to do this is to bring up the 'Connect to Database can add some useful extra scripting functionality that is not available with T-SQL. If you don't then the query may not be run against the correct server. When you run that batch file it will create a new script named all.sql in the same directory where the batch file is located. It will look for all files with the extension .sql in the same directory where the batch file is located.

SQL Server Utilities Statements - GO, SSMS provides us with an ability to set different colors for connection to separate To generate scripts for your database tables, SQL Server Also, think about a scenario when you run a TSQL query and SQL This is one of the hidden features of SSMS and you can actually Helpers and best practices. These parts are concatenated into a single string at compile time. There is no run-time performance cost regardless of the number of strings involved. // Concatenation of literals is performed at compile time, not run time. string text = "Historically, the world of data and the world of objects " + "have not been well integrated.

EXECUTE (Transact-SQL), GO signals the end of a batch of Transact-SQL statements to the SQL Server utilities. The batch preceding GO will execute the specified number of times. or since the start of the ad hoc session or script if this is the first GO. The statements in the batch are then compiled into a single execution plan. Hello ! I'm creating a application on vb.net with a sql server database. I have a situation when on runtime I have several sql queries ( Update and delete ) that are generated and I want to excute them with one database hit.

Understanding and Using Parallelism in SQL Server, Executes a command string or character string within a Transact-SQL batch, or one of the following modules: system stored procedure,� Make the script file executable after saving. The script method is better than using the pipe method so you can debug the script in stages, or later expand on it when you have more junk you need to cleanup.