title: Using a stored procedure as a "dynamic" view?
body: <p>I want to create a "view" to eliminate the same three-line sub-query from about 90 queries in an app I'm working on.</p>
  
  <p>The problem is the sub-query contains a condition based on a variable.</p>
  
  <pre><code>SELECT * FROM items WHERE id NOT IN (
    SELECT item_id FROM excluded_items WHERE user_id = 123
  );
  </code></pre>
  
  <p>If it wasn't variable, I could simply make a view and be done with it.</p>
  
  <p>I'm not sure what to do in this case though.  Adopting the same mentality behind a view I'm tempted to make a stored procedure that returns the desired record set, so that it could be called something like this:</p>
  
  <pre><code>SELECT * FROM user_items(123);
  </code></pre>
  
  <p>Now I have a single place to update this item exclusion and any further conditions, however I'm not sure how indexing is affected if I want to join the results of that SP against other tables?</p>
  
  <p>So is this good/bad practice?  Is there another way to do it, or should I just suck it up and keep replicating this sub-query?</p>