We have 3 tables in the database:
- [Unapproved].[Data]
- [Approved].[Data]
- [History].[Data]
We currently have 3 stored procedures that each run the same select query to get the data from the tables but the only difference is the schema.
What would be an optimal way of doing this say in one stored procedure (preferably without dynamic SQL)
There is no optimal way.
You can use an IF .. ELSE IF construct to decide which one to choose.
But they are 3 different objects so this is expected. I’d suggest that you’re not using schemas correctly…
From dba.se: