SQL [转换为bool]

时间:2010-01-22 06:45:35

标签: sql sql-server tsql stored-procedures casting

C ++ Builder ADOQuery SQLServer

我正在使用带有此选择的存储过程

SELECT  Name,
        COALESCE(
        (
        SELECT  TOP 1 0
        FROM    TbUserParam
        WHERE   TbUserParam.ID_User = @ID_User
                AND TbUserParam.ID_Param = CfgListParIzm.ID_ListParIzm
        ), 1) Visi
FROM    CfgListParIzm
WHERE   CfgListParIzm.ID_ListGroupParIzm = @ID_ListGroupParIzm

Stuff about this query

在我的查询中使用SQL字符串:

FlowClientHardQ  :ID_User, :ID_ListGroupParIzm

然后使用CheckBox字段的DataSource和DBGrid。 Source

所以我需要一个来自我的ADO查询的bool(或位)字段。 我如何/在哪里转换它?

谢谢。

2 个答案:

答案 0 :(得分:9)

施展到位?

SELECT  Name,
        CAST(
        COALESCE(
        (
        SELECT  TOP 1 0
        FROM    TbUserParam
        WHERE   TbUserParam.ID_User = @ID_User
                AND TbUserParam.ID_Param = CfgListParIzm.ID_ListParIzm
        ), 1) as bit) Visi
FROM    CfgListParIzm
WHERE   CfgListParIzm.ID_ListGroupParIzm = @ID_ListGroupParIzm

但是你不能重写查询,以避免子查询:

SELECT
    C.Name,
    CAST(CASE WHEN T2.ID_Param IS NULL THEN 0 ELSE 1 END as bit) AS Visi
FROM
    CfgListParIzm C
    LEFT JOIN
    (
    SELECT
        T.ID_Param
    FROM
        TbUserParam T
    WHERE
        T.ID_User = @ID_User
    ) T2 On T2.ID_Param = C.ID_ListParIzm
WHERE
    C.ID_ListGroupParIzm = @ID_ListGroupParIzm

答案 1 :(得分:3)

也许是一个CAST来咬?

SELECT CAST(COALESCE(NULL,1) AS BIT)

<强>更新  您也可以使用ISNULL()。 SELECT CAST(ISNULL(NULL,1) AS BIT)