title: Making a view change based on year without performance hit
body: <p>I have a view that looks at the four most recent years. It's pretty complex with 18 subqueries. It runs fast when the years are hard coded into the statement, but I would rather have it as a view that changes based on the year (School year, actually).</p>
  
  <p>I wrote a function that grabbed the correct year (not the current year) and replaced the hard coded years, and it went from &lt;30 seconds to more than I would wait. I also tried selecting the correct year, and it still didn't finish. I then tried making CTEs containing the tables with that year, and it still didn't finish.</p>
  
  <p>I feel that, if variables were allowed in view, I could just store the year in the variable and change the hard coded dates to that (or @var - 1, etc...) and not take a performance hit. Is there a way to do this using views without a huge execution time increase? Or do I need to switch it to a stored procedure?</p>
  
  <p>I haven't done a ton of SQL so I'm still trying to figure out if I'm missing something here. I don't see what this is causing such a huge performance change.</p>
  
  <p>Thanks.</p>