存储过程返回NULL而不是0

时间:2016-06-27 08:40:45

标签: stored-procedures sql-server-2012

我有一个问题 - 当我尝试执行此SELECT语句时,我没有收到任何内容。我想收到两列的0值。当我尝试使用ISNULL()或COALESCE()但没有发生任何事情时。这是SELECT语句:

SELECT 
    ACI.codinv AS codinv
    , ISNULL(COUNT(distinct ACI.appln_id), 0) AS CountInd
From    
    i_applnid_codinv aci 
    Inner Join i_applndata d On aci.appln_id = d.appln_id 
    Inner Join
        (Select x.appln_id, Count(x.codinv) As Count_codinv
            From   
            i_applnid_codinv x
            Group By x.appln_id
            Having Count(x.codinv) = 2) ac2 On ac2.appln_id = aci.appln_id
Where
    aci.codinv = 2222   
Group by
    ACI.codinv

2 个答案:

答案 0 :(得分:0)

不能修改你的查询。试试如下..

伪码:

select * from orderstest where empid=200 --this doesnt result any values

if @@rowcount=0
select orderid,custid from orderstest where empid=200
union all
select  top 1 null,null from orderstest

<强>输出:

orderid  custid
null     null

答案 1 :(得分:0)

你没有结果。检查这样的事情......

 IF (SELECT COUNT(*) From    
            i_applnid_codinv aci 
            Inner Join i_applndata d On aci.appln_id = d.appln_id 
            Inner Join
                (Select x.appln_id, Count(x.codinv) As Count_codinv
                    From   
                    i_applnid_codinv x
                    Group By x.appln_id
                    Having Count(x.codinv) = 2) ac2 On ac2.appln_id = aci.appln_id
        Where
            aci.codinv = 2222   
        Group by
            ACI.codinv) > 0
    BEGIN
        SELECT 
                ACI.codinv AS codinv
                , ISNULL(COUNT(distinct ACI.appln_id), 0) AS CountInd
            From    
                i_applnid_codinv aci 
                Inner Join i_applndata d On aci.appln_id = d.appln_id 
                Inner Join
                    (Select x.appln_id, Count(x.codinv) As Count_codinv
                        From   
                        i_applnid_codinv x
                        Group By x.appln_id
                        Having Count(x.codinv) = 2) ac2 On ac2.appln_id = aci.appln_id
            Where
                aci.codinv = 2222   
            Group by
                ACI.codinv
    END
    ELSE SELECT 0,0