Google "dynamic SQL".
Basically, instead of writing queries against fixed table names, you will write a stored procedure that will use T-SQL to write the queries into text strings, and you will then execute the query text strings. By having the stored procedure write the queries, they can be dynamically created at execution time.