Empty statement in T-SQL

后端 未结 5 1028
遇见更好的自我
遇见更好的自我 2020-12-09 15:32

Is there an empty statement keyword in T-SQL in Sql Server 2005 or newer? Something like NULL statement in PL/SQL.

5条回答
  •  眼角桃花
    2020-12-09 16:08

    ugly happens sometimes. I believe their is a valid use. In a lengthy/complicated decision branching structure with several if else statements, some of those statements may contain conditions in which you specifically desire no action. You also don't want those condition falling thru do the default else, where certain work is done. In that case, it's a valid use.

    Here are two ways to do this - see B and C

    Declare @status as char(1) 
    set @status = 'D'
    
    If (@status = 'A')
        select 'Great!'
    
    Else if (@status = 'B')
    begin
        if null=null select null -- predicate never resolves true
    end
    
    Else if (@status = 'C')
        set @status = @status  -- set a variable to itself 
    
    Else
        select 'Needs work!'
    

    Note, this is an over-simplified example. It is best used for readability when conditions are complex.

提交回复
热议问题