2016-11-17 75 views
0

功能:标量值函数为空,但IAM期待总和(标记)

create function fn_with_parameter 
(
    @id int=null 
) 
returns int 
with returns null on null input 
as 
    begin 
    declare @total int 
    select @total=sum(marks) from test3 
    where [email protected] 
    return @total 
    end 

选择:

select dbo.fn_with_parameter(default); 
+0

可能你详细说明你的问题?只是复制/粘贴代码不能够让SO社区帮助你。 –

+0

'空输入返回null','@id int = null','dbo.fn_with_parameter(默认)' - ??? –

+0

@PareshJ代码正常..但输出为空 –

回答

0

修改你的函数为:

create function fn_with_parameter 
(
@id int=null 
) 
returns int 
--with returns null on null input 
as 
begin 
declare @total int=0 
if isnull(@id,'') = '' 
set @id = '' 

select @total=sum(marks) from test3 
where isnull(id,'')[email protected] 
return isnull(@total,0) 
end 

select dbo.fn_with_parameter(default);