问题
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?
回答1:
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
回答2:
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
来源:https://stackoverflow.com/questions/48076590/can-i-restrict-sql-server-to-execute-code-only-when-some-code-is-selected