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??
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.
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.
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.
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.
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.
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
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With