views:

123

answers:

2

C++Builder ADOQuery SQLServer

I'm using a stored procedure with this select

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

in my query with SQL string :

FlowClientHardQ  :ID_User, :ID_ListGroupParIzm

then DataSource and DBGrid with CheckBox fields. Source

So I need a bool (or bit) field there from my ADO Query. How / Where can I convert it ?

Thank you.

+5  A: 

Cast to bit?

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

But can you not rewrite the query thus to avoid the subquery:

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
gbn
yes it works , thank you , but I still got some strange trouble somewhere "Cannot find the field Visi" (only on run program) ...thank you also for avoid subquery !
nCdy
+1  A: 

Maybe a CAST to BIT?

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

UPDATE You could use is ISNULL() as well. SELECT CAST(ISNULL(NULL,1) AS BIT)

Liao