简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM