title: Calling table-valued-function for each result in query
body: <p>Say I had a query like this:</p>
  
  <pre><code>SELECT X FROM Table WHERE Y = 'Z'
  </code></pre>
  
  <p>How could I execute a Stored Procedure using each <code>X</code> from the above query as the parameter?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I have changed the SP to be a Table-valued function instead. So for each call to the function it will return a table. What I need to do is store all these results in perhaps a temp table and have my SP return this table.</p>
  
  <p><strong>SOLUTION</strong></p>
  
  <p>Finally managed to get this to work with some help from @cyberkiwi. Here is my final solution:</p>
  
  <pre><code>DECLARE @Fields TABLE (
      Field int)
  
  INSERT INTO @Fields (X) SELECT * FROM tvf_GetFields(@SomeIdentifier)
  SELECT * FROM @Fields
  CROSS APPLY dbo.tvf_DoSomethingWithEachField([@Fields].Field)
  </code></pre>