12

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??

5 Answers 5

31

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.

Sign up to request clarification or add additional context in comments.

1 Comment

In my case issue was not using "dbo", but I don't really get those rules anyway.
4

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

Comments

2

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

Comments

1

Or if you do want a table function, try changing your function to be something like this - then you can use select * from...

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

    INSERT @outputTbl_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


GO

Comments

1
ALTER FUNCTION Isnulldate(@maxdate1 DATETIME, 
                          @maxdate2 DATETIME, 
                          @maxdate3 DATETIME) 
returns DATETIME 
AS 
  BEGIN 
      DECLARE @date DATETIME 

      IF @maxdate3 IS NOT NULL 
        BEGIN 
            SET @date=@maxdate3 

            RETURN @date 
        END 

      IF @maxdate2 IS NOT NULL 
        BEGIN 
            SET @date=@maxdate2 

            RETURN @date 
        END 

      IF @maxdate1 IS NOT NULL 
        BEGIN 
            SET @date=@maxdate1 

            RETURN @date 
        END 

      RETURN @date 
  END 

## Execution ##

DECLARE @dateim DATETIME=Getdate() 
SELECT dbo.Isnulldate(NULL, NULL, @dateim) 

1 Comment

While this code may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.