Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid Object Name Error in Function in SQL

I have following function defined

alter  FUNCTION [dbo].[GetXMLValues](@business_id int, @id varchar(30))
RETURNS varchar(30)
AS
BEGIN

declare @xmlValue varchar(30)

set @xmlValue =  (SELECT top 1000  T.Content.value('(/XmlDataPairDocument/dataitem[@id=sql:variable("@id")]/@value)[1]', 'VARCHAR(100)')
                    FROM tblApplications T where t.business_id =@business_id)


return @xmlValue




END

WHen i hit F5 command Executes Successfully/...

but when i try to execute it using following query :

select * from [GetXMLValues](1,'sadfj')

it shows an error saying : Invalid object name 'GetXMLValues'.

what is the reason ? and what is error??

like image 802
ghanshyam.mirani Avatar asked Jul 31 '13 10:07

ghanshyam.mirani


People also ask

Why does SQL say invalid object name?

Because the Connection currently points to the database containing the stored procedure and the table does not exist in that database, "Invalid Object Name" errors are returned.

Why do I get invalid column name in SQL Server?

An invalid column name error in SQL means that the column name violates the conditions of the column name. If you reference an object that does not exist in the table or the name exists, but you did not reference it correctly, you will get this error.

Could not use view or function because of binding errors in SQL Server?

when deploying changes to our systems it is not uncommon to get this error message Could not use view or function because of binding errors. This happens because a view can become outdated because of changes to the underlying objects upon which the view depends.

What is incorrect syntax near in SQL?

When executing a query in SQL and the editor throws back this error: Incorrect syntax near …'' That typically means you have used the wrong syntax for the query. This happens mostly when someone switched from one relational database to another relational database, from MySQL to MS SQL Server for example.


3 Answers

This is a Scalar function, not a Table-Valued function.

select dbo.[GetXMLValues](1,'sadfj')

should work.

You can't treat this like a table, i.e. select * ..., you need to just select the result directly as above.

See Types of Functions for more details.

like image 54
Ian Preston Avatar answered Oct 20 '22 09:10

Ian Preston


As mentioned by t-clausen.dk and Ian Preston, it's because you have a Scalar function and not a table valued function.

I just wanted to extend on t-clausen.dk's post which switches your function to a multi-statement table valued function. I would take this a step further and actually use an inline table valued function:

ALTER FUNCTION [dbo].[GetXMLValues](@business_id int, @id varchar(30))
RETURNS TABLE
AS
RETURN (
    SELECT top 1000  T.Content.value('(/XmlDataPairDocument/dataitem[@id=sql:variable("@id")]/@value)[1]',     'VARCHAR(100)')
    FROM tblApplications T where t.business_id =@business_id
)

Which you then use in the same way:

select xmlValue from dbo.[GetXMLValues](1,'sadfj')

Check out: Query performance and multi-statement table valued functions

like image 45
AdaTheDev Avatar answered Oct 20 '22 10:10

AdaTheDev


your function is not returning a table, it is returning a varchar(30). The correct syntax to use your function would be:

select [dbo].[GetXMLValues](1,'sadfj')

Try function this instead:

ALTER FUNCTION [dbo].[GetXMLValues](@business_id int, @id varchar(30))
RETURNS @t table (xmlValue varchar(30))
AS
BEGIN

insert @t (xmlValue)
SELECT top 1000  T.Content.value('(/XmlDataPairDocument/dataitem[@id=sql:variable("@id")]/@value)[1]', 'VARCHAR(100)')
FROM tblApplications T where t.business_id =@business_id

return
end

Then you can call your function this way:

select xmlValue from dbo.[GetXMLValues](1,'sadfj')
like image 25
t-clausen.dk Avatar answered Oct 20 '22 10:10

t-clausen.dk