sqlrver的判断语句

更新时间:2023-06-18 10:03:24 阅读: 评论:0

sqlrver的判断语句
⽅案⼀、使⽤ CASE 关键字
s开头的英文单词SELECT CAST(
CASE
WHEN Obsolete = 'N'or InStock = 'Y'
THEN1
ELSE0
END AS bit) as Saleable, *
FROM Product
以上语句返回的是⼀个布尔值,如何需要返回 int 类型呢,如下:
toutSELECT CASE
mgp
WHEN Obsolete = 'N'or InStock = 'Y'
THEN1
ELSE0
END as Saleable, *
FROM Product
⽅案⼆、使⽤ IIF 关键字
由于IIF 关键字是SQL Server 2012及上以版本才有的,所以本⽅案仅适合SQL Server 2012及以上版本,代码如下:SELECT IIF(Obsolete = 'N'or InStock = 'Y', 1, 0) as Saleable, * FROM Product
⽅案三
SELECT
(CASE
oilily
WHEN (Obsolete = 'N'OR InStock = 'Y') THEN'YES'
ELSE'NO'
END) as Salable
, *
FROM Product
⽅案四
IF EXISTS(SELECT *
FROM  Northwind.dbo.Customers
WHERE  CustomerId = 'ALFKI')
PRINT 'Need to update Customer Record ALFKI'
the scarlet letter
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'
⽅案六
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
zub'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
)disappeared
float是什么意思
SELECT
'Salable' = c.[CheckIfInstock] & ~c.[CheckIfObsolote]
,*
FROM
[cte] c
⽅案七
SELECT1AS Saleable, *
FROM @Product
WHERE ( Obsolete = 'N'OR InStock = 'Y' )
UNION
SELECT0AS Saleable, *
FROM @Product
WHERE NOT ( Obsolete = 'N'OR InStock = 'Y' )

本文发布于:2023-06-18 10:03:24,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/90/149237.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:返回   版本   语句   上以   代码   判断   需要
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图