Stored procedures 存储过程返回NULL而不是0

Stored procedures 存储过程返回NULL而不是0,stored-procedures,sql-server-2012,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

我有一个问题-当我试图执行这个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

如果不修改您的查询,则无法完成。请尝试以下操作

伪代码:

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
输出:

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

如果不修改您的查询,则无法完成。请尝试以下操作

伪代码:

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
输出:

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

你没有结果。检查一下这样的东西

 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

你没有结果。检查一下这样的东西

 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

您得到的输出是什么。where子句是否有匹配项?输出仅与标题匹配。在本例中没有匹配项,因此我希望为两列显示0和0!这是可能的吗?您确定您的内部联接没有导致数据抑制吗?更改为left JOIN以查看是否开始看到结果。我尝试了left JOIN和left OUTER JOIN,但没有任何结果。您得到的输出是什么。where子句是否有任何匹配项?输出仅与标题匹配。在本例中没有匹配项,因此我希望为两列显示0和0!这是可能的吗?您确定您的内部联接没有导致数据抑制吗?更改为“左连接”以查看是否开始看到结果。我尝试了“左连接”和“左外部连接”,但没有任何结果谢谢您的帖子!谢谢你的帖子!谢谢你的帖子!谢谢你的帖子!