mssql which is the syntax 'if'

This question already has an answer here:

  • How do I perform an IF…THEN in an SQL SELECT? 22 answers

  • You need to use IIF (SQL Server 2012 and above):

    IIF ( boolean_expression, true_value, false_value )

    SELECT
        t1.Id,
        t2.Id,
        t1.QuantityIn,
        t1.PriceIn,
        t2.QuantityOut,
        IIF (t2.QuantityOut - t1.QuantityIn=0,t2.QuantityOut
            , t2.QuantityOut - t1.QuantityIn ) AS Quant,
        t2.PriceOut
    FROM t1
    LEFT JOIN t2 ON t2.Id = t1.Id
    

    尝试这个

    SELECT
        t1.Id,
        t2.Id,
        t1.QuantityIn,
        t1.PriceIn,
        t2.QuantityOut,
    
        CASE WHEN t2.QuantityOut - t1.QuantityIn =0
    
            THEN t2.QuantityOut
    
        Else t2.QuantityOut - t1.QuantityIn END AS Quant,
    
        t2.PriceOut
    
    FROM t1
    
    LEFT JOIN t2 ON t2.Id = t1.Id
    
    链接地址: http://www.djcxy.com/p/94354.html

    上一篇: IF,ELIF,ELSE在T

    下一篇: mssql这是语法'if'