如何在SQL SELECT中执行IF … THEN?

如何在SQL SELECT语句中执行IF … THEN?

例如:

SELECT IF(Obsolete = 'N' OR InStock = 'Y' ? 1 : 0) AS Saleable, * FROM Product 

CASE语句在SQL中最接近IF,并且在所有版本的SQL Server上都受支持

 SELECT CAST( CASE WHEN Obsolete = 'N' or InStock = 'Y' THEN 1 ELSE 0 END AS bit) as Saleable, * FROM Product 

如果你想把结果作为一个布尔值,你只需要做CAST ,如果你对int很满意,就可以这样工作:

 SELECT CASE WHEN Obsolete = 'N' or InStock = 'Y' THEN 1 ELSE 0 END as Saleable, * FROM Product 

CASE语句可以embedded到其他CASE语句中,甚至包含在聚合中。

SQL Server Denali(SQL Server 2012)添加了访问中也可用的IIF语句:(由Martin Smith指出)

 SELECT IIF(Obsolete = 'N' or InStock = 'Y', 1, 0) as Saleable, * FROM Product 

在这种情况下,案件陈述是你的朋友,并采取以下两种forms之一:

简单的情况:

 SELECT CASE <variable> WHEN <value> THEN <returnvalue> WHEN <othervalue> THEN <returnthis> ELSE <returndefaultcase> END AS <newcolumnname> FROM <table> 

扩展的情况下:

 SELECT CASE WHEN <test> THEN <returnvalue> WHEN <othertest> THEN <returnthis> ELSE <returndefaultcase> END AS <newcolumnname> FROM <table> 

你甚至可以把case语句放在一个order by子句中,以进行真正有趣的sorting。

从SQL Server 2012可以使用IIFfunction 。

 SELECT IIF(Obsolete = 'N' OR InStock = 'Y', 1, 0) AS Salable, * FROM Product 

这实际上只是一种简写(尽pipe不是标准的SQL)写CASE

与扩展的CASE版本相比,我更喜欢简洁。

IIF()CASE都可以在SQL语句中以expression式的forms进行parsing,并且只能在明确定义的位置使用。

CASEexpression式不能用于控制Transact-SQL语句,语句块,用户定义的函数和存储过程的执行stream程。

如果你的需求不能满足这些限制(例如需要返回不同形状的结果集依赖于某些条件),那么SQL Server也有一个程序化的IF关键字。

 IF @IncludeExtendedInformation = 1 BEGIN SELECT A,B,C,X,Y,Z FROM T END ELSE BEGIN SELECT A,B,C FROM T END 

但是,有时候必须注意避免使用这种方法的参数嗅探问题。

你可以在SQL CASE语句的力量中find一些很好的例子,我认为你可以使用的语句是这样的(来自4guysfromrolla ):

 SELECT FirstName, LastName, Salary, DOB, CASE Gender WHEN 'M' THEN 'Male' WHEN 'F' THEN 'Female' END FROM Employees 

使用CASE。 像这样的东西。

 SELECT Salable = CASE Obsolete WHEN 'N' THEN 1 ELSE 0 END 
 SELECT (CASE WHEN (Obsolete = 'N' OR InStock = 'Y') THEN 'YES' ELSE 'NO' END) as Salable , * FROM Product 
  SELECT CASE WHEN OBSOLETE = 'N' or InStock = 'Y' THEN 'TRUE' ELSE 'FALSE' END AS Salable, * FROM PRODUCT 

Microsoft SQL Server(T-SQL)

在select使用:

 select case when Obsolete = 'N' or InStock = 'Y' then 'YES' else 'NO' end 

在where子句中,使用:

 where 1 = case when Obsolete = 'N' or InStock = 'Y' then 1 else 0 end 

从这个链接中 ,我们可以在T-SQL理解IF THEN ELSE

 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' 

这对T-SQL来说不够好吗?

在SQL Server中简单的if-else语句:

 DECLARE @val INT; SET @val = 15; IF @val < 25 PRINT 'Hi Ravi Anand'; ELSE PRINT 'By Ravi Anand.'; GO 

在SQL Server中嵌套如果… else语句 –

 DECLARE @val INT; SET @val = 15; IF @val < 25 PRINT 'Hi Ravi Anand.'; ELSE BEGIN IF @val < 50 PRINT 'what''s up?'; ELSE PRINT 'Bye Ravi Anand.'; END; GO 

使用纯位逻辑:

 DECLARE @Product TABLE ( id INT PRIMARY KEY IDENTITY NOT NULL ,Obsolote CHAR(1) ,Instock CHAR(1) ) INSERT INTO @Product ([Obsolote], [Instock]) VALUES ('N', 'N'), ('N', 'Y'), ('Y', 'Y'), ('Y', 'N') ; WITH cte AS ( SELECT 'CheckIfInstock' = CAST(ISNULL(NULLIF(ISNULL(NULLIF(p.[Instock], 'Y'), 1), 'N'), 0) AS BIT) ,'CheckIfObsolote' = CAST(ISNULL(NULLIF(ISNULL(NULLIF(p.[Obsolote], 'N'), 0), 'Y'), 1) AS BIT) ,* FROM @Product AS p ) SELECT 'Salable' = c.[CheckIfInstock] & ~c.[CheckIfObsolote] ,* FROM [cte] c 

请参阅工作演示:如果没有MSSQL的情况下

首先,您需要计算所选条件的true值和false值。 这里有两个NULLIF :

 for true: ISNULL(NULLIF(p.[Instock], 'Y'), 1) for false: ISNULL(NULLIF(p.[Instock], 'N'), 0) 

结合在一起给出1或0.接下来使用按位运算符 。

这是最见所见即所得的方法。

使用CASE语句:

 SELECT CASE WHEN (Obsolete = 'N' OR InStock = 'Y') THEN 'Y' ELSE 'N' END as Available etc... 

在SQL Server 2012中添加了一个新functionIIF (我们可以简单地使用它):

 SELECT IIF ( (Obsolete = 'N' OR InStock = 'Y'), 1, 0) AS Saleable, * FROM Product 
 SELECT 1 AS Saleable, * FROM @Product WHERE ( Obsolete = 'N' OR InStock = 'Y' ) UNION SELECT 0 AS Saleable, * FROM @Product WHERE NOT ( Obsolete = 'N' OR InStock = 'Y' ) 
 SELECT CASE WHEN profile.nrefillno = 0 THEN 'N' ELSE 'R'END as newref From profile 

如果您是第一次将结果插入表中,而不是将结果从一个表转移到另一个表,那么在Oracle 11.2g中可以使用这个结果:

 INSERT INTO customers (last_name, first_name, city) SELECT 'Doe', 'John', 'Chicago' FROM dual WHERE NOT EXISTS (SELECT '1' from customers where last_name = 'Doe' and first_name = 'John' and city = 'Chicago'); 
 case statement some what similar to if in SQL server SELECT CASE WHEN Obsolete = 'N' or InStock = 'Y' THEN 1 ELSE 0 END as Saleable, * FROM Product 

这不是一个答案,只是我在工作中使用的CASE语句的一个例子。 它有一个嵌套的CASE语句。 现在你知道我的眼睛为什么越过了。

  CASE orweb2.dbo.Inventory.RegulatingAgencyName WHEN 'Region 1' THEN orweb2.dbo.CountyStateAgContactInfo.ContactState WHEN 'Region 2' THEN orweb2.dbo.CountyStateAgContactInfo.ContactState WHEN 'Region 3' THEN orweb2.dbo.CountyStateAgContactInfo.ContactState WHEN 'DEPT OF AGRICULTURE' THEN orweb2.dbo.CountyStateAgContactInfo.ContactAg ELSE ( CASE orweb2.dbo.CountyStateAgContactInfo.IsContract WHEN 1 THEN orweb2.dbo.CountyStateAgContactInfo.ContactCounty ELSE orweb2.dbo.CountyStateAgContactInfo.ContactState END ) END AS [County Contact Name] 
  SELECT IIF(Obsolete = 'N' OR InStock = 'Y',1,0) AS Saleable, * FROM Product 

对于那些使用SQL Server 2012的用户来说,IIF是一个已添加的function,可用作Case语句的替代方法。

 SELECT IIF(Obsolete = 'N' OR InStock = 'Y', 1, 0) AS Salable, * FROM Product 
 SELECT CASE WHEN Obsolete = 'N' or InStock = 'Y' THEN 1 ELSE 0 END AS Saleable, * FROM Product 
 CASE WHEN CAST([PartnerProg Start Date] AS DATE) < CAST('1-Nov-2010' AS DATE) AND CAST([PartnerProg End Date] AS DATE) > CAST('31-Jan-2011' AS DATE) THEN 'Preferred or Gold' ELSE '' END AS 'Partner Segment 2 Q111', 

如果您想比较多个date,请使用此选项