title: SQL Server: run function for each row based on the provided row value
body: <p>I am still learning SQL and I was wandering how can I pass a row value into a function in a <code>cross apply</code> query. </p>
  
  <p>Assuming that this is my first table</p>
  
  <pre><code>SELECT 
      [Project], [emp_id],
      [Allocation_start], [Allocation_end]
  FROM
      First_table
  </code></pre>
  
  <p>I have a function that takes 2 arguments - start and end date, and returns dates split by the week intervals...</p>
  
  <p>Example : </p>
  
  <pre><code>select * from [udf_GetIntervals]('2017-01-01','2017-01-30')
  </code></pre>
  
  <p>Data:</p>
  
  <pre><code>dt_start    dt_end
  ----------------------
  2016-12-26  2017-01-01 
  2017-01-02  2017-01-08 
  2017-01-09  2017-01-15 
  2017-01-16  2017-01-22 
  2017-01-23  2017-01-29 
  2017-01-30  2017-02-05 
  </code></pre>
  
  <p>What I did was I run the mentioned function with dates starting from the 1st July to the 30th December and stored it in virtual table and then used cross apply with that table.</p>
  
  <pre><code>select * 
  from [First_table] 
  cross apply
      (select * from #temp) b
  </code></pre>
  
  <p>Which works, but it works independently of the <code>first_tables</code> start and end date always returning all weeks per 1 project record from first table.</p>
  
  <p>I was wondering, how can i do this, using first_tables start_date and end_date values, so that my cross apply returns only records with the week_intervals contained in the rows start/end date.</p>
  
  <p>I would appreciate any hints on the matter. </p>