I was wondering what the best way was to query multiple databases in a stored procedure. I can hard code the database names in the stored procedure i.e:
Select *
from [Audit].[dbo].[AuditTable] inner join
[Core].[dbo].[CoreTable]
on ....
But I would like to store the stored procedures in a version control system and then migrate them to different environments. So I was wondering the best way to do this. The database name is different for different environments.
Possible solutions:
- Using a view to span tables in different databases? The database name will only exist in one place
- Extended properties for the database/ passing the name into a stored procedure. It is possible to dynamically change the name of the database used in a stored procedure: Changing database name in a stored procedure dynamically But I am worried this will be messy
- Separate environments with the same database name across all environments?
It seems that this is a duplicate of: Cross-database queries with different DB names in different environments