Query across multiple databases on same server

前端 未结 4 2142
感动是毒
感动是毒 2020-12-14 06:34

I am looking for a way of dealing with the following situation:

  1. We have a database server with multiple databases on it (all have the same schema, different

4条回答
  •  一个人的身影
    2020-12-14 07:38

    It's not going to be the cleanest solution ever, but you could define a view on a "Master database" (if your individual databases are not going to stay constant) that includes the data from the individual databases, and allows you to execute queries on a single source.

    For example...

    CREATE VIEW vCombinedRecords AS
    SELECT * FROM DB1.dbo.MyTable
    UNION ALL
    SELECT * FROM DB2.dbo.MyTable
    

    Which allows you to do...

    SELECT * FROM vCombinedRecords WHERE....
    

    When your databases change, you just update the view definition to include the new tables.

提交回复
热议问题