title: Return dynamic SQL table from function or stored procedure to use in a query
body: <p>I have a number of tables that have a "Deleted" field of type DATETIME that I use to mark records as deleted (instead of totally deleting them).</p>
  
  <p>I often find myself requesting data from these tables and wanting to exclude the deleted records. What I am doing right now is something like:</p>
  
  <pre><code>SELECT A.* 
  FROM (SELECT * FROM [TargetTable] WHERE (Deleted IS NULL)) AS A
  WHERE (.........)
  </code></pre>
  
  <p>Is it possible to have a stored procedure/function to which I can pass the table name and have the filtered table returned? Something like:</p>
  
  <pre><code>SELECT A.*
  FROM fnExcludeDeleted('MyTable') AS A
  WHERE (.........)
  </code></pre>
  
  <p>I tried doing it using a table valued function but from what I could gather, I would need to define the resulting table's columns. I cannot do this because it is not always the same table that I am using the function on.</p>