58

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'.

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'. "

where i made error . can anyone help me...

0

5 Answers 5

117

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] 
Sign up to request clarification or add additional context in comments.

1 Comment

Must specify the schema as shown, not just the function name. I had to retry several times due to this. Hope it helps someone.
27

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.

select dbo.fun_functional_score('01091400003') 

Comments

9

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') 

1 Comment

See that link: stackoverflow.com/questions/4995442/… It seems that the same applies when there are parameters
3

For Scalar Function Syntax is

Select dbo.Function_Name(parameter_name) Select dbo.Department_Employee_Count('HR') 

Comments

0

If you tried the top answer and it still doesn't work, then there may be something wrong with the function. In my case, the function referred to a table that didn't exist anymore, and instead of the error message telling me what was really happening, it just gave me the same error message as above.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.