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

筅森魡賤 提交于 2019-12-11 02:54:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!