Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I restrict SQL Server to execute code only when some code is selected?

The other day I was trying to hit another button on the menu but hit Execute - which executed the whole code and ended up deleting some tables. I have always found this scary that hitting one button can execute the whole code.

I want SQL Server to execute code only when something is selected. Is it possible? Or can SQL Server prompt before executing a query?

like image 314
sachin01663 Avatar asked Feb 14 '26 03:02

sachin01663


2 Answers

If you wrap your code in NOEXEC then you effectively stop the code from running unless you select the code inside them.
The code will compile but will not execute!

Example:

SET NOEXEC ON;

SELECT 1 AS Test;

SET NOEXEC OFF;

If you run the whole code, nothing will be returned.

If you highlight just the SELECT part then it will run though, returning:

Test
----
1
like image 176
Shaneis Avatar answered Feb 16 '26 20:02

Shaneis


In MSSQL Management Studio, the Execute button (F5) executes only the sentences that are selected. If nothing is selected, then executes the complete script.

I allways select the sentences I want to execute. But it's my habit. Nothing will prevent you from pressing F5 directly

like image 32
mnieto Avatar answered Feb 16 '26 22:02

mnieto