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

前端 未结 30 1864
梦如初夏
梦如初夏 2020-11-21 22:50

How do I perform an IF...THEN in an SQL SELECT statement?

For example:

SELECT IF(Obsolete = \'N\' OR InStock = \'Y\' ? 1 :          


        
30条回答
  •  无人及你
    2020-11-21 23:10

    From this link, we can understand IF THEN ELSE in T-SQL:

    IF EXISTS(SELECT *
              FROM   Northwind.dbo.Customers
              WHERE  CustomerId = 'ALFKI')
      PRINT 'Need to update Customer Record ALFKI'
    ELSE
      PRINT 'Need to add Customer Record ALFKI'
    
    IF EXISTS(SELECT *
              FROM   Northwind.dbo.Customers
              WHERE  CustomerId = 'LARSE')
      PRINT 'Need to update Customer Record LARSE'
    ELSE
      PRINT 'Need to add Customer Record LARSE' 
    

    Isn't this good enough for T-SQL?

提交回复
热议问题