如何使用case时从sql返回true或false

时间:2013-10-25 19:25:30

标签: tsql case-when

我试图根据一个CASE WHEN THEN THEN语句返回true或false,但结果面板中唯一显示的是列名“IsGeneric”。

我哪里错了?

alter proc Storefront.proc_IsProjectGeneric

@ProjectID INT
AS
SET NOCOUNT ON;

SELECT 'IsGeneric'=CASE WHEN p.[GenericCatalogID] > 0 THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT) END
                 FROM Storefront.Project p WITH(NOLOCK)
                 WHERE p.ID = @ProjectID;

SET NOCOUNT OFF;

2 个答案:

答案 0 :(得分:16)

你在标识符周围使用了撇号,这使得它成为一个字符串。

SELECT IsGeneric = CASE WHEN p.[GenericCatalogID] > 0 THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT) END
             FROM Storefront.Project p WITH(NOLOCK)
             WHERE p.ID = @ProjectID;

答案 1 :(得分:5)

SELECT CASE WHEN p.[GenericCatalogID] > 0 
            THEN CAST(1 AS BIT) 
            ELSE CAST(0 AS BIT) 
       END as IsGeneric