在SQL Select语句上返回布尔值 [英] Return Boolean Value on SQL Select Statement
本文介绍了在SQL Select语句上返回布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在SQL Select语句上返回布尔值?
How to return a boolean value on SQL Select Statement?
我尝试了以下代码:
SELECT CAST(1 AS BIT) AS Expr1
FROM [User]
WHERE (UserID = 20070022)
并且仅当表上存在UserID
时才返回TRUE
.如果表中不存在UserID
,我希望它返回FALSE
.
And it only returns TRUE
if the UserID
exists on the table. I want it to return FALSE
if the UserID
doesn't exist on the table.
推荐答案
如果用户不存在,那么您所拥有的将根本不会返回任何行.这就是您需要的:
What you have there will return no row at all if the user doesn't exist. Here's what you need:
SELECT CASE WHEN EXISTS (
SELECT *
FROM [User]
WHERE UserID = 20070022
)
THEN CAST(1 AS BIT)
ELSE CAST(0 AS BIT) END
这篇关于在SQL Select语句上返回布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文