0

I have a function that has two outputs ...

dbo.func1(@code) -> Table(out1, out2)

This function is too costly and takes much time to calculate these two outputs. and I have a query like this :

SELECT code, name,
(SELECT out1 dbo.func1(code)), (SELECT out2 dbo.func1(code))
FROM MyInnerJoinedTablesResult

But my costly function is call for two time but I want to call it one time for each record selected in my table... and result in two column in any row (not double rows)

SELECT code, name,
(out1 in func1), (out2 in func2)
FROM MyInnerJoinedTablesResult
Atzi
  • 457
  • 1
  • 6
  • 16
  • 3
    `SELECT t.code, f.out1, f.out2 FROM dbo.table AS t CROSS APPLY dbo.func1(t.code) AS f` – Aaron Bertrand Sep 08 '14 at 20:47
  • Sorry i can't understand what you try to accomplish. Maybe some sample would help. – ericpap Sep 08 '14 at 20:48
  • And make sure your table valued function is an inline table valued function. That means there is 1 and ONLY 1 statement in the function. If you have variables and all sort of processing your performance is not going to be any better. – Sean Lange Sep 08 '14 at 21:38

1 Answers1

1

You need to use Cross Apply

SELECT code, name, func.out1, func.out2
FROM MyInnerJoinedTablesResult
cross apply dbo.func1(code) as func
Jeffrey Wieder
  • 2,336
  • 1
  • 14
  • 12