1

Apologies if this has been asked before, but the case is not so usual.

I work with a lot with a single .sql file with many helper queries in it, example:

select * from elmah_error order by timeutc desc;
select * from orders order by timeutc desc;

I want to prevent whole file execution with F5 so that only "selection" is executed upon pressing F5.

I looked around SQL Server Management Studio for any setting that could prevent "full file execution" and couldn't find any.

Is there and EXIT/BREAK/GOTOTOP T-SQL command I could use at the top of each file?

stop;

select * from elmah_error order by timeutc desc;
select * from orders order by timeutc desc;

Many thanks.

mehdi lotfi
  • 11,194
  • 18
  • 82
  • 128
Pedro Costa
  • 2,968
  • 2
  • 18
  • 30

1 Answers1

5

If you do not have any batches executing in your file but only sql statements i.e Sql Statements are not separated with GO, you can modify your file using GOTO and skip the statement you do not want to be executed.

But if you do have sql statement in separate batches then this will not work.

/* Execute the following Statements */

PRINT 'Statement 1 executed'
PRINT 'Statement 2 executed'

GOTO SKIP_3;   --<-- this will skip the statement 3 and will jump to "SKIP_3: Label" 

/* Skip Statement 3 */
PRINT 'Statement 3 executed'

SKIP_3:                   
PRINT 'Statement 4 executed'

GOTO END_EXIT;          --<-- Stop script execution here and jump to end

/* Execute Statement 5 and 6 */
PRINT 'Statement 5 executed'
PRINT 'Statement 6 executed'

END_EXIT:

Result

Statement 1 executed
Statement 2 executed
Statement 4 executed
M.Ali
  • 67,945
  • 13
  • 101
  • 127