是否有一种方法可以立即停止SQL服务器中SQL脚本的执行,如“break”或“exit”命令?

我有一个脚本,它在开始插入之前执行一些验证和查找,我希望它在任何验证或查找失败时停止。


当前回答

raiserror方法

raiserror('Oh no a fatal error', 20, -1) with log

这将终止连接,从而停止脚本的其余部分的运行。

注意,要以这种方式工作,严重级别20或更高级别和WITH LOG选项都是必需的。

这甚至适用于GO语句,例如。

print 'hi'
go
raiserror('Oh no a fatal error', 20, -1) with log
go
print 'ho'

将给你输出:

hi
Msg 2745, Level 16, State 2, Line 1
Process ID 51 has raised user error 50000, severity 20. SQL Server is terminating this process.
Msg 50000, Level 20, State 1, Line 1
Oh no a fatal error
Msg 0, Level 20, State 0, Line 0
A severe error occurred on the current command.  The results, if any, should be discarded.

注意没有打印'ho'。

警告:

这只在您以admin ('sysadmin'角色)身份登录时有效,并且没有数据库连接。 如果您不是以管理员身份登录,则RAISEERROR()调用本身将失败,脚本将继续执行。 当使用sqlcmd.exe调用时,将报告退出代码2745。

参考:http://www.mydatabasesupport.com/forums/ms - sqlserver/174037 - sql - server - 2000 -中止整个script.html # post761334

noexec方法

另一个使用GO语句的方法是set noexec on (docs)。这将导致跳过脚本的其余部分。它不会终止连接,但在执行任何命令之前,您需要再次关闭noexec。

例子:

print 'hi'
go

print 'Fatal error, script will not continue!'
set noexec on

print 'ho'
go

-- last line of the script
set noexec off -- Turn execution back on; only needed in SSMS, so as to be able 
               -- to run this script again in the same session.

其他回答

这是存储过程吗?如果是这样,我认为你可以只做一个返回,如“返回NULL”;

你可以使用GOTO语句。试试这个。这对你很有用。

WHILE(@N <= @Count)
BEGIN
    GOTO FinalStateMent;
END

FinalStatement:
     Select @CoumnName from TableName

在SQL 2012+中,您可以使用THROW。

THROW 51000, 'Stopping execution because validation failed.', 0;
PRINT 'Still Executing'; -- This doesn't execute with THROW

从MSDN:

引发异常并将执行转移到TRY…CATCH构造的CATCH块…如果TRY…CATCH结构不可用,则会话结束。设置引发异常的行号和过程。级别设置为16。

将它包含在try catch块中,然后执行将被转移到catch。

BEGIN TRY
    PRINT 'This will be printed'
    RAISERROR ('Custom Exception', 16, 1);
    PRINT 'This will not be printed'
END TRY
BEGIN CATCH
    PRINT 'This will be printed 2nd'
END CATCH;

我用一个事务成功地扩展了noexec开/关解决方案,以全有或全无的方式运行脚本。

set noexec off

begin transaction
go

<First batch, do something here>
go
if @@error != 0 set noexec on;

<Second batch, do something here>
go
if @@error != 0 set noexec on;

<... etc>

declare @finished bit;
set @finished = 1;

SET noexec off;

IF @finished = 1
BEGIN
    PRINT 'Committing changes'
    COMMIT TRANSACTION
END
ELSE
BEGIN
    PRINT 'Errors occured. Rolling back changes'
    ROLLBACK TRANSACTION
END

显然,编译器“理解”IF中的@finished变量,即使有一个错误并且执行被禁用。但是,只有在未禁用执行时,该值才会设置为1。因此,我可以很好地提交或回滚事务。