Cross-database name change in SQL Server 2008 stored procedures

5 pts.
Tags:
SQL 2008
SQL Server 2008 R2
SQL Server stored procedures
Stored Procedures
Hi:

The easy part is the databases will be on the same server.  However at the moment I am writing code in database SOMEDATABASE_TEST which is accessing objects in SOMEOTHERDATABASE_TEST.  When its time to go to production the names are going to change to SOMEDATABASE and SOMEOTHERDATABASE.  However the queries in SOMEDATABASE_TEST stored procedures read:

select * from SOMEOTHERDATABASE_TEST.dbo.SOMETABLE

the goal would be to have some variable @@crossdatabasename = SOMEOTHERDATABASE_TEST

then all the calls would look like

select * from @@crossdatabasename.dbo.SOMETABLE

We are using sql server 2008 r2

What is the best way around this?

Thanks



Software/Hardware used:
sql server 2008 r2

Answer Wiki

Thanks. We'll let you know when a new response is added.

Notwithstanding a redesign to bring all related objects into the same database, I would think you can get where you want to be using SYNONYMs.

–Use this for TestDB
CREATE SYNONYM DBOther
FOR SOMEOTHERDATABASE_TEST.dbo.SOMETABLE
GO

–Use this for ProdDB
CREATE SYNONYM DBOther
FOR SOMEOTHERDATABASE.dbo.SOMETABLE
GO

–Write your generic code using the SYNONYM
SELECT *
FROM DBOther
GO

Discuss This Question:  

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following