2014-04-22 104 views
-1

我已經在SQL 2008中創建了一個SP,它的工作正常,當我執行它顯示出來,但我希望如果記錄不在表中,那麼它會顯示0或全部列中的空值。如何從存儲過程SQL 2008服務器得到空值

它在簡單的選擇查詢中工作正常,但是當我調用存儲過程時它不顯示任何東西。

Alter proc [dbo].[catewiseexp] @awpyear varchar(50) =null , 
    @divisionid int=null 
    As 
    begin 
    declare @res as numeric(18,2) =null 

    select 
    /* Financial Progress C1 */ 
    isnull(CONVERT(DECIMAL(10,2),sum(CASE WHEN [email protected] and cateid=1 THEN (IDA)/100000 ELSE 0 END)),0) as ReIDA, 
    isnull(CONVERT(DECIMAL(10,2),sum(CASE WHEN [email protected] and cateid=1 THEN (Govt)/100000 ELSE 0 END)),0) as ShStat, 
    isnull(CONVERT(DECIMAL(10,2),sum(CASE WHEN [email protected] and cateid=1 THEN (Benyfe)/100000 ELSE 0 END)),0) as BeneContro, 
    isnull(CONVERT(DECIMAL(10,2),sum(CASE WHEN [email protected] and cateid=1 THEN (IDA+Govt+Benyfe)/100000 ELSE 0 END)),0) as Total, 
    /* Financial Progress C2  */ 
    isnull(CONVERT(DECIMAL(10,2),sum(CASE WHEN [email protected] and cateid=2 THEN (IDA)/100000 ELSE 0 END)),0) as ReIDA2, 
    isnull(CONVERT(DECIMAL(10,2),sum(CASE WHEN [email protected] and cateid=2 THEN (Govt)/100000 ELSE 0 END)),0) as ShStat2, 
    Isnull(CONVERT(DECIMAL(10,2),sum(CASE WHEN [email protected] and cateid=2 THEN (Benyfe)/100000 ELSE 0 END)),0) as BeneContro2, 
    isnull(CONVERT(DECIMAL(10,2),sum(CASE WHEN [email protected] and cateid=2 THEN (IDA+Govt+Benyfe)/100000 ELSE 0 END)),0) as Total2, 
    /* Financial Progress NR  */ 

COALESCE(CONVERT(DECIMAL(10,2),sum(CASE WHEN [email protected] and cateid=0 THEN (Govt)/100000 ELSE 0 END)),0) as NR, 

/* Financial Progress Total IDA  */ 
isnull(CONVERT(DECIMAL(10,2),sum(CASE WHEN [email protected] THEN (IDA)/100000 ELSE 0 END)),0) as TotIDA, 

/* Financial Progress tot State Share */ 
isnull(CONVERT(DECIMAL(10,2),sum(CASE WHEN [email protected] and cateid<>0 THEN (Govt)/100000 ELSE 0 END)),0) as TotStat, 

/* Financial Progress tot State Share with NR */ 
Isnull(CONVERT(DECIMAL(10,2),sum(CASE WHEN [email protected] THEN (Govt)/100000 ELSE 0 END)),0) as TotStatNR, 

/* Financial Progress Beneficiary Contro  */ 
isnull(CONVERT(DECIMAL(10,2),sum(CASE WHEN [email protected] THEN (Benyfe)/100000 ELSE 0 END)),0) as TotBenyfe, 

/* Financial Progress total */ 
isnull(CONVERT(DECIMAL(10,2),sum(CASE WHEN [email protected] THEN (IDA+Govt+Benyfe)/100000 ELSE 0 END)),0) as TotFin 


from AWP 
where [email protected] 
group by Division 
end 

回答

0

你可以嘗試這樣的事情......我們首先檢查是否有該部門的任何記錄,如果沒有,我們再次選擇零

Alter proc [dbo].[catewiseexp] @awpyear varchar(50) =null , 
@divisionid int=null 
As 
begin 
declare @res as numeric(18,2) =null 

IF EXISTS(SELECT * FROM AWP WHERE [email protected]) 

    select 
    /* Financial Progress C1 */ 
    isnull(CONVERT(DECIMAL(10,2),sum(CASE WHEN [email protected] and cateid=1 THEN (IDA)/100000 ELSE 0 END)),0) as ReIDA, 
    isnull(CONVERT(DECIMAL(10,2),sum(CASE WHEN [email protected] and cateid=1 THEN (Govt)/100000 ELSE 0 END)),0) as ShStat, 
    isnull(CONVERT(DECIMAL(10,2),sum(CASE WHEN [email protected] and cateid=1 THEN (Benyfe)/100000 ELSE 0 END)),0) as BeneContro, 
    isnull(CONVERT(DECIMAL(10,2),sum(CASE WHEN [email protected] and cateid=1 THEN (IDA+Govt+Benyfe)/100000 ELSE 0 END)),0) as Total, 
    /* Financial Progress C2  */ 
    isnull(CONVERT(DECIMAL(10,2),sum(CASE WHEN [email protected] and cateid=2 THEN (IDA)/100000 ELSE 0 END)),0) as ReIDA2, 
    isnull(CONVERT(DECIMAL(10,2),sum(CASE WHEN [email protected] and cateid=2 THEN (Govt)/100000 ELSE 0 END)),0) as ShStat2, 
    Isnull(CONVERT(DECIMAL(10,2),sum(CASE WHEN [email protected] and cateid=2 THEN (Benyfe)/100000 ELSE 0 END)),0) as BeneContro2, 
    isnull(CONVERT(DECIMAL(10,2),sum(CASE WHEN [email protected] and cateid=2 THEN (IDA+Govt+Benyfe)/100000 ELSE 0 END)),0) as Total2, 
    /* Financial Progress NR  */ 

    COALESCE(CONVERT(DECIMAL(10,2),sum(CASE WHEN [email protected] and cateid=0 THEN (Govt)/100000 ELSE 0 END)),0) as NR, 

    /* Financial Progress Total IDA  */ 
    isnull(CONVERT(DECIMAL(10,2),sum(CASE WHEN [email protected] THEN (IDA)/100000 ELSE 0 END)),0) as TotIDA, 

    /* Financial Progress tot State Share */ 
    isnull(CONVERT(DECIMAL(10,2),sum(CASE WHEN [email protected] and cateid<>0 THEN (Govt)/100000 ELSE 0 END)),0) as TotStat, 

    /* Financial Progress tot State Share with NR */ 
    Isnull(CONVERT(DECIMAL(10,2),sum(CASE WHEN [email protected] THEN (Govt)/100000 ELSE 0 END)),0) as TotStatNR, 

    /* Financial Progress Beneficiary Contro  */ 
    isnull(CONVERT(DECIMAL(10,2),sum(CASE WHEN [email protected] THEN (Benyfe)/100000 ELSE 0 END)),0) as TotBenyfe, 

    /* Financial Progress total */ 
    isnull(CONVERT(DECIMAL(10,2),sum(CASE WHEN [email protected] THEN (IDA+Govt+Benyfe)/100000 ELSE 0 END)),0) as TotFin 

    from AWP 
    where [email protected] 
    group by Division 
else 
    select 
    /* Financial Progress C1 */ 
    0 as ReIDA, 
    0 as ShStat, 
    0 as BeneContro, 
    0 as Total, 
    /* Financial Progress C2  */ 
    0 as ReIDA2, 
    0 as ShStat2, 
    0 as BeneContro2, 
    0 as Total2, 
    /* Financial Progress NR  */ 
    0 as NR, 
    /* Financial Progress Total IDA  */ 
    0 as TotIDA, 
    /* Financial Progress tot State Share */ 
    0 as TotStat, 
    /* Financial Progress tot State Share with NR */ 
    0 as TotStatNR, 
    /* Financial Progress Beneficiary Contro  */ 
    0 as TotBenyfe, 
    /* Financial Progress total */ 
    0 as TotFin 

end 
+0

敬愛它爲我工作表示感謝。 ................ –

+0

您可以將問題標記爲已回答嗎? – Brendan