SQL Server中的T-SQL STOP或ABORT命令

时间:2010-01-08 14:10:04

标签: sql sql-server tsql sql-scripts

Microsoft SQL Server T-SQL中是否有命令告诉脚本停止处理? 我有一个脚本,我想保留用于存档目的,但我不希望任何人运行它。

9 个答案:

答案 0 :(得分:44)

另一种解决方案可能是使用GOTO语句来改变脚本的执行流程......

DECLARE  @RunScript bit;
SET @RunScript = 0;

IF @RunScript != 1
BEGIN
RAISERROR ('Raise Error does not stop processing, so we will call GOTO to skip over the script', 1, 1);
GOTO Skipper -- This will skip over the script and go to Skipper
END

PRINT 'This is where your working script can go';
PRINT 'This is where your working script can go';
PRINT 'This is where your working script can go';
PRINT 'This is where your working script can go';

Skipper: -- Don't do nuttin!

警告!以上样本来自我从Merrill Aldrich获得的一个例子。在您盲目实施GOTO声明之前,我建议您阅读Flow control in T-SQL Scripts上的教程。

答案 1 :(得分:35)

不,没有 - 你有几个选择:

  1. 将整个脚本包装在一个很大的if / end块中,这个块确保不成立(即“如果1 = 2开始”) - 这只会在脚本不包含任何GO语句的情况下起作用(因为那些表示新批次)

  2. 使用顶部的return语句(再次,由批处理分隔符限制)

  3. 使用基于连接的方法,这将确保整个脚本不执行(整个连接更准确) - 在顶部使用'SET PARSEONLY ON''SET NOEXEC ON'之类的内容剧本。这将确保连接中的所有语句(或直到所述set语句关闭)将不会执行,而是仅被解析/编译。

  4. 使用注释块注释掉整个脚本(即/ *和* /)

  5. 编辑:证明'return'语句是特定于批处理的 - 注意在返回后你将继续看到结果集:

    select 1
    return
    go
    select 2
    return
    select 3
    go
    select 4
    return
    select 5
    select 6
    go
    

答案 2 :(得分:16)

为什么不简单地将以下内容添加到脚本的开头

PRINT 'INACTIVE SCRIPT'
RETURN

答案 3 :(得分:14)

要解决RETURN / GO问题,您可以将RAISERROR ('Oi! Stop!', 20, 1) WITH LOG置于顶部。

这将按照RAISERROR on MSDN关闭客户端连接。

非常的缺点是你必须是系统管理员才能使用严重性20。

编辑:

一个简单的示威来反击泽西·杜德的评论......

RAISERROR ('Oi! Stop!', 20, 1)  WITH LOG
SELECT 'Will not run'
GO
SELECT 'Will not run'
GO
SELECT 'Will not run'
GO

答案 4 :(得分:8)

严重性为20的RAISERROR将在事件查看器中报告为错误。

您可以使用SET PARSEONLY ON; (或NOEXEC)。在脚本结束时使用GO SET PARSEONLY OFF;

SET PARSEONLY ON;
-- statement between here will not run

SELECT 'THIS WILL NOT EXEC';

GO
-- statement below here will run

SET PARSEONLY OFF;

答案 5 :(得分:3)

尝试将其作为TSQL脚本运行

SELECT 1
RETURN
SELECT 2
SELECT 3

返回结束执行。

RETURN (Transact-SQL)

  

无条件退出查询或   程序。立即返回   完成,可以随时使用   退出过程,批处理或   声明块。声明   跟随RETURN未执行。

答案 6 :(得分:3)

通过使用"全球"以及使用"全球"变量

if object_id('tempdb..#vars') is not null
begin
  drop table #vars
end

create table #vars (continueScript bit)
set nocount on
  insert #vars values (1)
set nocount off

-- Start of first batch
if ((select continueScript from #vars)=1) begin

  print '1'

  -- Conditionally terminate entire script
  if (1=1) begin
    set nocount on
      update #vars set continueScript=0
    set nocount off
    return
  end

end
go

-- Start of second batch
if ((select continueScript from #vars)=1) begin

  print '2'

end
go

这与每个GO批次的事务和try / catch块使用的想法相同。您可以尝试更改各种条件和/或让它生成错误(除以0,请参阅注释)以测试其行为:

if object_id('tempdb..#vars') is not null
begin
  drop table #vars
end

create table #vars (continueScript bit)
set nocount on
  insert #vars values (1)
set nocount off

begin transaction;
  -- Batch 1 starts here
  if ((select continueScript from #vars)=1) begin
    begin try 
      print 'batch 1 starts'

      if (1=0) begin
        print 'Script is terminating because of special condition 1.'
        set nocount on
          update #vars set continueScript=0
        set nocount off
        return
      end

      print 'batch 1 in the middle of its progress'

      if (1=0) begin
        print 'Script is terminating because of special condition 2.'
        set nocount on
          update #vars set continueScript=0
        set nocount off
        return
      end

      set nocount on
        -- use 1/0 to generate an exception here
        select 1/1 as test
      set nocount off

    end try
    begin catch
      set nocount on
        select 
          error_number() as errornumber
          ,error_severity() as errorseverity
          ,error_state() as errorstate
          ,error_procedure() as errorprocedure
          ,error_line() as errorline
          ,error_message() as errormessage;
        print 'Script is terminating because of error.'
        update #vars set continueScript=0
      set nocount off
      return
    end catch;

  end
  go

  -- Batch 2 starts here
  if ((select continueScript from #vars)=1) begin

    begin try 
      print 'batch 2 starts'

      if (1=0) begin
        print 'Script is terminating because of special condition 1.'
        set nocount on
          update #vars set continueScript=0
        set nocount off
        return
      end

      print 'batch 2 in the middle of its progress'

      if (1=0) begin
        print 'Script is terminating because of special condition 2.'
        set nocount on
          update #vars set continueScript=0
        set nocount off
        return
      end

      set nocount on
        -- use 1/0 to generate an exception here
        select 1/1 as test
      set nocount off

    end try
    begin catch
      set nocount on
        select 
          error_number() as errornumber
          ,error_severity() as errorseverity
          ,error_state() as errorstate
          ,error_procedure() as errorprocedure
          ,error_line() as errorline
          ,error_message() as errormessage;
        print 'Script is terminating because of error.'
        update #vars set continueScript=0
      set nocount off
      return
    end catch;

  end
  go

if @@trancount > 0 begin
  if ((select continueScript from #vars)=1) begin
    commit transaction
    print 'transaction committed'
  end else begin
    rollback transaction;
    print 'transaction rolled back'
  end
end

答案 7 :(得分:2)

尽管它的描述非常明确且有力,但RETURN在存储过程中并不适用于我(为了进一步执行)。我不得不修改条件逻辑。发生在SQL 2008,2008 R2上:

create proc dbo.prSess_Ins
(
    @sSessID    varchar( 32 )
,   @idSess     int out
)
as
begin
    set nocount on

    select  @id=    idSess
        from    tbSess
        where   sSessID = @sSessID

    if  @idSess > 0 return  -- exit sproc here

    begin   tran
        insert  tbSess  ( sSessID ) values  ( @sSessID )
        select  @idSess=    scope_identity( )
    commit
end

必须改为:

    if  @idSess is null
    begin
        begin   tran
            insert  tbSess  ( sSessID ) values  ( @sSessID )
            select  @idSess=    scope_identity( )
        commit
    end

发现重复的行后发现。调试PRINT确认@idSess在IF检查中的值大于零 - RETURN没有中断执行!

答案 8 :(得分:1)

我知道问题是陈旧的,并且在几个不同的方面得到了正确的回答,但我没有在类似情况下使用的答案。 第一种方法(非常基本):

IF (1=0)
BEGIN
    PRINT 'it will not go there'
    -- your script here
END
PRINT 'but it will here'

第二种方法:

PRINT 'stop here'
RETURN
    -- your script here
PRINT 'it will not go there'

您可以自行测试,以确保其表现符合预期。