Return Boolean Value on SQL Select Statement

后端 未结 9 935
不知归路
不知归路 2021-01-29 21:36

How to return a boolean value on SQL Select Statement?

I tried this code:

SELECT CAST(1 AS BIT) AS Expr1
FROM [User]
WHERE (UserID = 20070022)

9条回答
  •  佛祖请我去吃肉
    2021-01-29 21:57

    I do it like this:

    SELECT 1 FROM [dbo].[User] WHERE UserID = 20070022
    

    Seeing as a boolean can never be null (at least in .NET), it should default to false or you can set it to that yourself if it's defaulting true. However 1 = true, so null = false, and no extra syntax.

    Note: I use Dapper as my micro orm, I'd imagine ADO should work the same.

提交回复
热议问题