SQLServer-停⽌或中断SQL脚本的执⾏
本⽂翻译⾃:
Is there a way to immediately stop execution of a SQL script in SQL server, like a "break" or "exit" command? 有没有办法⽴即
有没有办法⽴即停⽌在SQL Server中执⾏SQL脚本,如“break”或“exit”命令?
I have a script that does some validation and lookups before it starts doing inserts, and I want it to stop if any of the validations or lookups fail. 我有⼀个脚本在开始插⼊之前执⾏⼀些验证和查,我希望它在任何验证或查失败时停⽌。
我有⼀个脚本在开始插⼊之前执⾏⼀些验证和查,我希望它在任何验证或查失败时停⽌。
#1楼
#2楼
This was my solution: 这是我的解决⽅案:
这是我的解决⽅案:
... ......
BEGIN
raiserror('Invalid database', 15, 10)
rollback transaction
return
END
#3楼
If you are simply executing a script in Management Studio, and want to stop execution or rollback transaction (if used) on first error, then the best way I reckon is to use try catch block (SQL 2005 onward). 如果您只是在Management Studio中执
如果您只是在Management Studio中执⾏脚本,并且想要在第⼀次错误时停⽌执⾏或回滚事务(如果
使⽤),那么我认为最好的⽅法是使⽤try catch块(SQL 2005以
如果您正在执⾏脚本⽂件,这在Management 后)。 This works well in Management studio if you are executing a script file. 如果您正在执⾏脚本⽂件,这在Management 后)。
存储过程也可以始终使⽤它。
studio中运⾏良好。
studio中运⾏良好。 Stored proc can always use this as well. 存储过程也可以始终使⽤它。
#4楼
If you can use SQLCMD mode, then the incantation 如果你可以使⽤SQLCMD模式,那么咒语
如果你可以使⽤SQLCMD模式,那么咒语
:on error exit
(INCLUDING the colon) will cause RAISERROR to actually stop the script. (包括冒号)将导致RAISERROR实际停⽌脚本。
(包括冒号)将导致RAISERROR实际停⽌脚本。Eg, 例如,
try catch的使用方法例如,
:on error exit
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SOMETABLE]') AND type in (N'U'))
RaisError ('This is not a Valid Instance Database', 15, 10)
GO
print 'Keep Working'
will output: 将输出:
将输出:
Msg 50000, Level 15, State 10, Line 3
This is not a Valid Instance Database
** An error was encountered during execution of batch. Exiting.
and the batch will stop. 批次将停⽌。
如果未打开批次将停⽌。 If SQLCMD mode isn't turned on, you'll get parse error about the colon. 如果未打开SQLCMD模式,您将获得有关冒号的解析错误。
SQLCMD模式,您将获得有关冒号的解析错误。 Unfortuantely, it's not completely bulletproof as if the script is run without being in SQLCMD mode, SQL Managment Studio breezes right past even parse time errors! 不幸的是,它并不是完全没有防
不幸的是,它并不是完全没有防御性的,就好像脚本是在没有处于SQLCMD模式的情况下运⾏⼀样,SQL Managment Studio正好经过了解析时间错误!
御性的,就好像脚本是在没有处于SQLCMD模式的情况下运⾏⼀样,SQL Managment Studio正好经过了解析时间错误! Still, if you're running them from the command line, this is fine. 不过,如果你从命令⾏运⾏它们,这很好。
不过,如果你从命令⾏运⾏它们,这很好。
#5楼
You can use GOTO statement. 您可以使⽤GOTO语句。
这是完全适合你的。
试试这个。 This is use full for you. 这是完全适合你的。
您可以使⽤GOTO语句。 Try this. 试试这个。
WHILE(@N <= @Count)
BEGIN
GOTO FinalStateMent;
END
FinalStatement:
Select @CoumnName from TableName
#6楼
In SQL 2012+, you can use . 在SQL 2012+,您可以使⽤ 。
在SQL 2012+,您可以使⽤ 。
THROW 51000, 'Stopping execution because validation failed.', 0;
PRINT 'Still Executing'; -- This doesn't execute with THROW
From MSDN: 来⾃MSDN:
来⾃MSDN:
Raises an exception and transfers execution to a CATCH block of a TRY…CATCH construct ... If a TRY…CATCH construct is not available, the session is ended. 引发异常并将执⾏转移到TRY ... CATCH构造的CATCH块...如果TRY ...
引发异常并将执⾏转移到TRY ... CATCH构造的CATCH块...如果TRY ...
设置引发异常CATCH构造不可⽤,则会话结束。
CATCH构造不可⽤,则会话结束。 The line number and procedure where the exception is raised are
set. 设置引发异常
严重性设置为16。
的⾏号和过程。
的⾏号和过程。 The severity is set to 16. 严重性设置为16。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论