如何从存储过程sql 2008 server获取空值
我已经在sql 2008中创建了一个SP,当我执行它时,它工作正常。它显示输出,但我希望如果记录不在表中,那么它将在所有列中显示0或Null值 它在简单的select查询中运行良好,但当我调用存储过程时,它不会显示任何内容如何从存储过程sql 2008 server获取空值,sql,sql-server-2008,stored-procedures,Sql,Sql Server 2008,Stored Procedures,我已经在sql 2008中创建了一个SP,当我执行它时,它工作正常。它显示输出,但我希望如果记录不在表中,那么它将在所有列中显示0或Null值 它在简单的select查询中运行良好,但当我调用存储过程时,它不会显示任何内容 Alter proc [dbo].[catewiseexp] @awpyear varchar(50) =null , @divisionid int=null As begin declare @res as numeric(18,
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
你可以试试这样的。。。我们首先检查除法是否有任何记录,如果没有,我们选择零
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
那么你能把这个问题标为已经回答了吗?