Return Boolean Value on SQL Select Statement

后端 未结 9 902
不知归路
不知归路 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:43

    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
    

提交回复
热议问题