The only way to have a variable table name would be to use dynamic SQL. You can't use dynamic SQL in a function.
Can you use a proc instead of a function and have the return value changed to an output parameter?
Alternately, you might be able to create a view that selects the required data from all of the tables, and have the function select from the view. If you regularly add tables (which is a really, really bad idea), you could set up a job to alter the view as needed.
A better idea is to change the table structure so that the date is a column instead of part of the table name. That will prevent all kinds of problems.
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon