如何在sql server 2008中调用标量函数

时间:2014-01-10 10:06:27

标签: sql sql-server

我创建了一个标量函数,它已成功创建,但是当我使用select语句调用该函数时,它会显示无效的对象名称'dbo.fun_functional_score'。

我的职能:

 ALTER function [dbo].[fun_functional_score] (@phy_id varchar(20))
 returns  varchar(50)

as
begin 

declare @level_initial int, @level_current int

-- initial functional score
set @level_initial=(SELECT pflag.fun_level
FROM tbl_phy_demographic_details as [phy] 
    inner join tbl_all_purple_flag_level as [pflag] on phy.Demographic_id=pflag.Id 
WHERE phy.Physicion_id=@phy_id
    and pflag.visited_count=(select MAX(visited_count)-1 from tbl_all_purple_flag_level ))


-- current functional score
set @level_current=(SELECT pflag.fun_level
FROM tbl_phy_demographic_details as [phy] 
    inner join tbl_all_purple_flag_level as [pflag] on phy.Demographic_id=pflag.Id 
WHERE phy.Physicion_id=@phy_id
    and pflag.visited_count=(select MAX(visited_count) from tbl_all_purple_flag_level ))


--to calculate functional score
declare @fun_level varchar(20),@result varchar(50)

set  @fun_level=@level_current-@level_initial;

 if @fun_level = 0   set @result='Maintained' 
if @fun_level = '-1'  set @result='Minor Improvement' 
if @fun_level = '-2'  set @result='Moderate Improvement' 
if @fun_level = '-3'  set @result='Significant Improvement' 
if @fun_level =  '-4'  set @result='Substantial Improvement' 
if @fun_level =  '1'  set @result='Minor Reduction' 
if @fun_level =  '2'  set @result='Moderate Reduction' 
if @fun_level =  '3'  set @result='Significant Reduction' 
if @fun_level =  '4'  set @result='Substantial Reduction' 




return @result

end

我用这个选择来调用

 select * from dbo.fun_functional_score('01091400003') as [er]

 select * from dbo.fun_functional_score('01091400003') 

都显示错误“无效的对象名称'dbo.fun_functional_score'。 “

我犯了错误。任何人都可以帮助我...

4 个答案:

答案 0 :(得分:83)

您的语法是表值函数,它返回结果集并且可以像表一样查询。对于标量函数,请执行

 select  dbo.fun_functional_score('01091400003') as [er]

答案 1 :(得分:19)

你有一个标量值函数而不是表值函数。 from子句用于表。只需直接在列列表中查询该值。

select dbo.fun_functional_score('01091400003')

答案 2 :(得分:7)

出于某种原因,我在使用括号引用它之前无法使用我的标量函数,如下所示:

select [dbo].[fun_functional_score]('01091400003')

答案 3 :(得分:1)

标量函数 语法

{{1}}