Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
同时运行单个T-SQL脚本的各个部分的最佳方法_Sql_Sql Server_Tsql_Refactoring - Fatal编程技术网

同时运行单个T-SQL脚本的各个部分的最佳方法

同时运行单个T-SQL脚本的各个部分的最佳方法,sql,sql-server,tsql,refactoring,Sql,Sql Server,Tsql,Refactoring,我有一些很长的脚本,上面有变量。当我只想运行包含变量的部分脚本时,最好的方法是什么…除了以下选项之外,还有其他选项吗 复制/粘贴声明变量(但这需要我进行注释 (他们一直在进进出出) 只是将变量更改为硬编码(但我担心我会忘记将它们更改回来) 那么,有没有一种方法可以单独突出我想运行的部分…或者完全其他的部分 这个问题是针对SQL Server 2008的有时我会将部分代码包装在开始…结束块中,以便在SSM中展开和折叠它们。事实证明,如果0=1,可以在块前面加上,这样它就不会执行。扩展此想法,您可以

我有一些很长的脚本,上面有变量。当我只想运行包含变量的部分脚本时,最好的方法是什么…除了以下选项之外,还有其他选项吗

  • 复制/粘贴声明变量(但这需要我进行注释 (他们一直在进进出出)
  • 只是将变量更改为硬编码(但我担心我会忘记将它们更改回来)
  • 那么,有没有一种方法可以单独突出我想运行的部分…或者完全其他的部分


    这个问题是针对SQL Server 2008的

    有时我会将部分代码包装在
    开始…结束
    块中,以便在SSM中展开和折叠它们。事实证明,如果0=1,可以在块前面加上
    ,这样它就不会执行。扩展此想法,您可以这样做:

    -- variables
    DECLARE @p1 INT = 123;
    DECLARE @p2 INT = 456;
    
    -- which block to run
    DECLARE @block AS INT = 1;
    
    IF @block = 1
    BEGIN
        SELECT 'statement block 1'
    END
    
    IF @block = 2
    BEGIN
        SELECT 'statement block 2'
    END
    

    我倾向于使用一个我称之为
    @debug
    的特殊变量,但您可以随意调用它。当我调试时,我将使用它来设置变量、发出状态消息(伪装成错误消息)等等。然后,我只需要记住在完成时重置一个值

    DECLARE 
      @var1 INT,
      @var2 VARCHAR(25),
      @var3 BIGINT,
      @debug TINYINT,   --Indicator that I'm in debug mode
      @msg VARCHAR(50); --Custom message text placeholder
    
    SET @debug = 1;
    
    IF @debug = 1
      BEGIN
        SELECT
          @var1 = 1,
          @var2 = 'Hello World',
          @var3 = 12;
      END;
    
    SELECT @var1, @var2, @var3;
    
    IF @debug = 1
      BEGIN
        SET @msg = 'Step 1 Complete'
        RAISERROR(@msg, 1,0) WITH NOWAIT
      END;
    

    使用条件句。有关说明,请参见@RunOne和@RunTwo

    DECLARE @MessageText as varchar(28) = 'This is not a greeting'
    
    Declare @RunOne as bit = 1 -- set to 0 to skip first part
    Declare @RunTwo as bit = 1 -- set to 0 to skip second part
    
    IF @RunOne = 1
    BEGIN
       select @MessageText + ', or is it?'
    END
    
    IF @RunTwo = 1
    BEGIN
       select @MessageText + ' for wide dissemination.'
    END
    

    只是基于萨尔曼的答案(+1)

    这种扭曲将允许块范围,而不是一次一个

    示例

    -- Declare Your Variables
    Declare @V1 int = 8;
    Declare @V2 int = 12;
    
    -- Define which Blocks to Execute  
    Declare @BlockRange1 int = 3
    Declare @BlockRange2 int = 4
    
    
    If 1 between @BlockRange1 and @BlockRange2
    Begin
        Select 1 
    End
    
    If 2 between @BlockRange1 and @BlockRange2
    Begin
        Select 2
    End
    
    If 3 between @BlockRange1 and @BlockRange2
    Begin
        Select 3
    End
    
    If 4 between @BlockRange1 and @BlockRange2
    Begin
        Select 4
    End
    

    您可以这样标记查询的每个部分并使用子句:

        --Chose Witch Block To Run 
        DECLARE @ControlFlag SMALLINT = <YourChoose>
        IF ControlFlag = 1 GOTO PART1 ELSE GOTO PART2
        PART1:
        DECLARE @v1 INT,@v2 INT
    
        PART2:
        DECLARE @v3 INT,@v4 INT
    
    --选择要运行的开关块
    声明@ControlFlag SMALLINT=
    如果ControlFlag=1转到部件1,则转到部件2
    第1部分:
    声明@v1 INT、@v2 INT
    第2部分:
    声明@v3 INT、@v4 INT
    
    如果您可以/想要对脚本进行功能性修改,那么所有其他答案都可以,但如果您希望避免这种情况,正如我所怀疑的,我通常的做法是对我想跳过的部分进行块注释:

    所以我只是改变这个:

    DECLARE @variables;
    
    Some Code I want to skip this time;
    
    Some Code I want to execute this time;
    
    Maybe some more code I want to skip this time;
    
    为此:

    DECLARE @variables;
    /*
    Some Code I want to skip this time;
    */
    Some Code I want to execute this time;
    /*
    Maybe some more code I want to skip this time;
    */