title: Inline table-valued function vs Stored Procedure (SQL Server)
body: <p>I'm a bit confused about what is better to use in the following case:
  I have a quite complex query that is used for reporting purposes. A simplified version looks like </p>
  
  <pre><code>  SELECT [type], COUNT(*) as total_num, COUNT(DISTINCT user_id) as uq_user_num
    FROM table1
    INNER JOIN table2 ON (...)
    ...
    WHERE table3.last_action_date BETWEEN :start_date AND :end_date
    GROUP BY [type]
  </code></pre>
  
  <p>I can create an inline function or a stored procedure that takes start_date and end_parameters and executes this query.<br>
  I incline to function because this task does not involve any data modification or complex logic. Also, I may want to use the result in <code>APPLY</code> later (it's not really important at the moment).<br>
  Does it make sense to use function, not procedure? Is it any difference from performance point of view (execution plan caching, etc) ?</p>
  
  <p>Thank you.</p>