1

I have a stored procedure that needs to do an inner join on a function that returns a table. Trying to call it like so:

INNER JOIN [dbo].[udf_GetBillingInfo(@QuoteID)] billInfo ON quotes.QuoteID = billInfo.QuoteID

gives an error invalid object name dbo.udf_GetBillingInfo(@QuoteID)

Given my requirements I don't think I can just make the function into a view as it has a minor bit of logic in it. How can I join the result? Would I have to assign the function result to a variable or something of that nature?

Wayne Molina
  • 19,158
  • 26
  • 98
  • 163

3 Answers3

5

Your problem is the square brackets - [dbo].[udf_GetBillingInfo(@QuoteID)] - they quote whatever is inside as the function name.

Change it to this:

[dbo].udf_GetBillingInfo(@QuoteID)
cjk
  • 45,739
  • 9
  • 81
  • 112
0

Here is an (dumb) example of joining to a TVF, hope it helps!

CREATE FUNCTION MyFunc ( @p1 int ) RETURNS TABLE 
AS
RETURN 
(
        SELECT @p1 AS a 
        union 
        SELECT @p1 +1 AS a 
        union 
        SELECT @p1 +2 AS a
)
GO

declare @t table (c int) 
insert @t (c) values(1),(2),(3),(4)

select * 
from @t t 
left join dbo.MyFunc(1) f on  t.c = f.a
JohnD
  • 14,327
  • 4
  • 40
  • 53
-1

Can you write you function as a table function? I think you may find the answer here

tsql returning a table from a function or store procedure

Community
  • 1
  • 1
Keith Bloom
  • 2,391
  • 3
  • 18
  • 30