SQL Server Function Timeout - Although the select statement inside the function runs in 1 second
<p>SQL Server 2012</p>

<p>Can anyone shed some light on this bizare behavour?</p>

<p>I have a function that calls a very complicated select statement. It accepts 3 parameters</p>

<pre><code>Select * 
from Myfunction(@CompanyId, @DivisionId, @PersonId)
</code></pre>

<p>This times out after 10 minutes.</p>

<p>If I take the internal SQL of the function, declare the same variables, set them to the same values and run, this takes a second to return 8 records.</p>

<p>If I run the function with a <code>@CompanyId</code> that does not return results, I get no rows back as expected and is returned immediately (so it's not permissions issue).</p>

<p>We have just inserted a lot of data in for this function to deal with, but this does not explain why the select is working and the function hangs?</p>

<p>Any ideas?</p>