Large runtime difference for function with pivot calculation

Hans Furmann 40 Reputation points
2024-01-08T08:58:57.2133333+00:00

Hello,

I have a SQL Server function that outputs the results of a table function as a pivot table. If I run the pivot function directly, it takes 6 seconds, if I call this function as a SQL script with the same parameters, it takes 26 seconds. What could be the reason for this?
This is the function with 6 seconds execution time:

  

If I Run the above function this way, it uses 26 seconds:

SELECT * FROM [MyPivotFunction]('2024-01-01', '2024-12-31', 371)

Does anyone have an idea why there are such time differences with the SQL server?

Regards

Hans
SQL Server | Other
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.