我已经在sql 2008中创建了一个SP,当我执行它时它工作正常,显示输出但是我想如果记录不在表中那么它将在所有列中显示0或Null值。
它在简单的选择查询中工作正常但是当我调用存储过程时它没有显示任何东西。
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 awpyear=@awpyear and cateid=1 THEN (IDA)/100000 ELSE 0 END)),0) as ReIDA,
isnull(CONVERT(DECIMAL(10,2),sum(CASE WHEN awpyear=@awpyear and cateid=1 THEN (Govt)/100000 ELSE 0 END)),0) as ShStat,
isnull(CONVERT(DECIMAL(10,2),sum(CASE WHEN awpyear=@awpyear and cateid=1 THEN (Benyfe)/100000 ELSE 0 END)),0) as BeneContro,
isnull(CONVERT(DECIMAL(10,2),sum(CASE WHEN awpyear=@awpyear 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 awpyear=@awpyear and cateid=2 THEN (IDA)/100000 ELSE 0 END)),0) as ReIDA2,
isnull(CONVERT(DECIMAL(10,2),sum(CASE WHEN awpyear=@awpyear and cateid=2 THEN (Govt)/100000 ELSE 0 END)),0) as ShStat2,
Isnull(CONVERT(DECIMAL(10,2),sum(CASE WHEN awpyear=@awpyear and cateid=2 THEN (Benyfe)/100000 ELSE 0 END)),0) as BeneContro2,
isnull(CONVERT(DECIMAL(10,2),sum(CASE WHEN awpyear=@awpyear 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 awpyear=@awpyear and cateid=0 THEN (Govt)/100000 ELSE 0 END)),0) as NR,
/* Financial Progress Total IDA */
isnull(CONVERT(DECIMAL(10,2),sum(CASE WHEN awpyear=@awpyear THEN (IDA)/100000 ELSE 0 END)),0) as TotIDA,
/* Financial Progress tot State Share */
isnull(CONVERT(DECIMAL(10,2),sum(CASE WHEN awpyear=@awpyear 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 awpyear=@awpyear THEN (Govt)/100000 ELSE 0 END)),0) as TotStatNR,
/* Financial Progress Beneficiary Contro */
isnull(CONVERT(DECIMAL(10,2),sum(CASE WHEN awpyear=@awpyear THEN (Benyfe)/100000 ELSE 0 END)),0) as TotBenyfe,
/* Financial Progress total */
isnull(CONVERT(DECIMAL(10,2),sum(CASE WHEN awpyear=@awpyear THEN (IDA+Govt+Benyfe)/100000 ELSE 0 END)),0) as TotFin
from AWP
where Division=@divisionid
group by Division
end
答案 0 :(得分: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 Division=@divisionid)
select
/* Financial Progress C1 */
isnull(CONVERT(DECIMAL(10,2),sum(CASE WHEN awpyear=@awpyear and cateid=1 THEN (IDA)/100000 ELSE 0 END)),0) as ReIDA,
isnull(CONVERT(DECIMAL(10,2),sum(CASE WHEN awpyear=@awpyear and cateid=1 THEN (Govt)/100000 ELSE 0 END)),0) as ShStat,
isnull(CONVERT(DECIMAL(10,2),sum(CASE WHEN awpyear=@awpyear and cateid=1 THEN (Benyfe)/100000 ELSE 0 END)),0) as BeneContro,
isnull(CONVERT(DECIMAL(10,2),sum(CASE WHEN awpyear=@awpyear 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 awpyear=@awpyear and cateid=2 THEN (IDA)/100000 ELSE 0 END)),0) as ReIDA2,
isnull(CONVERT(DECIMAL(10,2),sum(CASE WHEN awpyear=@awpyear and cateid=2 THEN (Govt)/100000 ELSE 0 END)),0) as ShStat2,
Isnull(CONVERT(DECIMAL(10,2),sum(CASE WHEN awpyear=@awpyear and cateid=2 THEN (Benyfe)/100000 ELSE 0 END)),0) as BeneContro2,
isnull(CONVERT(DECIMAL(10,2),sum(CASE WHEN awpyear=@awpyear 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 awpyear=@awpyear and cateid=0 THEN (Govt)/100000 ELSE 0 END)),0) as NR,
/* Financial Progress Total IDA */
isnull(CONVERT(DECIMAL(10,2),sum(CASE WHEN awpyear=@awpyear THEN (IDA)/100000 ELSE 0 END)),0) as TotIDA,
/* Financial Progress tot State Share */
isnull(CONVERT(DECIMAL(10,2),sum(CASE WHEN awpyear=@awpyear 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 awpyear=@awpyear THEN (Govt)/100000 ELSE 0 END)),0) as TotStatNR,
/* Financial Progress Beneficiary Contro */
isnull(CONVERT(DECIMAL(10,2),sum(CASE WHEN awpyear=@awpyear THEN (Benyfe)/100000 ELSE 0 END)),0) as TotBenyfe,
/* Financial Progress total */
isnull(CONVERT(DECIMAL(10,2),sum(CASE WHEN awpyear=@awpyear THEN (IDA+Govt+Benyfe)/100000 ELSE 0 END)),0) as TotFin
from AWP
where Division=@divisionid
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