如何在 sql server 2008 中调用标量函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21041345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to call scalar function in sql server 2008
提问by Happy
I have created a Scalar Functions, it was created successfully, but when I call the function using select statement, it says Invalid object name 'dbo.fun_functional_score'.
我创建了一个标量函数,它已成功创建,但是当我使用 select 语句调用该函数时,它显示无效的对象名称“dbo.fun_functional_score”。
my function:
我的功能:
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
i used this select to call
我用这个选择打电话
select * from dbo.fun_functional_score('01091400003') as [er]
or
或者
select * from dbo.fun_functional_score('01091400003')
both showing error "Invalid object name 'dbo.fun_functional_score'. "
两者都显示错误“无效的对象名称'dbo.fun_functional_score'。”
where i made error . can anyone help me...
我犯错的地方。谁能帮我...
回答by Mudassir Hasan
Your syntax is for table valued function which return a resultset and can be queried like a table. For scalar function do
您的语法适用于返回结果集的表值函数,并且可以像表一样进行查询。对于标量函数做
select dbo.fun_functional_score('01091400003') as [er]
回答by Mikael Eriksson
You have a scalar valued function as opposed to a table valued function. The from clause is used for tables. Just query the value directly in the column list.
您有一个标量值函数,而不是表值函数。from 子句用于表。直接在列列表中查询值即可。
select dbo.fun_functional_score('01091400003')
回答by JohannGunn
For some reason I was not able to use my scalar function until I referenced it using brackets, like so:
出于某种原因,我无法使用我的标量函数,直到我使用括号引用它,如下所示:
select [dbo].[fun_functional_score]('01091400003')
回答by Rae Lee
For Scalar FunctionSyntax is
对于标量函数语法是
Select dbo.Function_Name(parameter_name)
Select dbo.Department_Employee_Count('HR')