Cross-database queries with different DB names in different environments?

前端 未结 2 1575
没有蜡笔的小新
没有蜡笔的小新 2021-01-16 01:41

How would you handle cross database queries in different environments. For example, db1-development and db2-development, db1-production and db2-production.

If I wa

2条回答
  •  鱼传尺愫
    2021-01-16 02:13

    For this reason, it's not practical to use different names for development and production databases. Using the same db name on development, production, and optionally, acceptance/Q&A environments, makes your SQL code much easier to maintain.

    However, if you really have to, you could get creative with views and dynamic SQL. For example, you put the actual data retrieval query inside a view, and then you select like this:

    declare @environment varchar(10)
    set @environment = 'db-dev' -- input parameter, comes from app layer
    
    declare @sql varchar(8000)
    set @sql = 'select * from ' + @environment + '.dbo.view'
    execute(@sql)
    

    But it's far from pretty...

提交回复
热议问题