简体   繁体   中英

SQL query execute with using schema

I have two database my_cf_1 and my_cf_2 .

I move two databases ( my_cf_1 , my_cf_2 ) to single database my_cf using schema.

my_cf database has two schema my_cf_1 and my_cf_2 .

Query example:

Old case (I have two databases)

USE my_cf_1

SELECT * 
FROM ORDERS O 
JOIN my_cf_2.DBO.COMPANY C ON O.COMPANY_ID = C.COMPANY_ID

New case (I have a single database with two schemas)

USE my_cf

SELECT * 
FROM my_cf_1.ORDERS O 
JOIN my_cf_2.COMPANY C ON O.COMPANY_ID = C.COMPANY_ID

My issue: I have a lot of queries. I must change all queries. So how can I execute query Use Database.SchemaName scope?

Is this possible?

USE my_cf.my_cf_1

SELECT * 
FROM ORDERS O 
JOIN my_cf_2.COMPANY C ON O.COMPANY_ID = C.COMPANY_ID

No that is not possible. Other than refactoring your entire codebase, you could drop all the tables in my_cf2 but it. online. You can then create synonyms pointing to the new tables. This will allow you to gradually refactor your codebase

USE my_cf_2
CREATE SYNONYM DBO.COMPANY FOR my_cf.my_cf2.COMPANY

With this synonym this query:

SELECT * from my_cf2.DBO.COMPANY

will be identical to:

SELECT * from my_cf.my_cf2.COMPAN

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM