views:

2886

answers:

2

I'm using ms sql 2008 and trying to create a database name that references another database. For example 'Dev', 'Test', 'Demo' would be database names that i could reference from my multiple config files, but each name would point to another database such as 'db20080101' or 'db20080114'.

[Edit]Some of the configs are for applications that i control the code and some aren't (ex. MS Reporting service datasource file configs)[/Edit]

It seems that sqlserver only supports synonyms for View,Table,Sproc, or Function. And Alias' are for table and column names.

Is there a way to do this that i missed in the docs? Any one have any suggestions on a workaround?

+2  A: 

use 3 part notation and alias up to the table, example

select * from tempdb.dbo.sysobjects a
join master.dbo.sysobjects b on a.id = b.id
SQLMenace
i tried this for one of the tables and it worked great. however, i believe i would have to create a synonym in the new db for each table, function, view, and sproc in the original db? i've got 5 or 6 config files, so i think changing them will be less overhead.
Kevin McKinley
+1  A: 

I've done something similar to this using another config file.

The new config file maps your generic name to all of the information needed to connect to that database (db name, user name, password, etc.) and then your connection function takes your generic name as an argument.

db.config:

DEV_DB_NAME = db20080101
DEV_DB_USER = dev_user
DEV_DB_PASS = dev_pass
TEST_DB_NAME = db20070101
TEST_DB_USER = test_user
TEST_DB_PASS = test_pass

connection code:

db_connection get_connection(string prefix) {
    db_connection db_conn = new db_connection;
    string db_name = get_config_value(config_path, prefix + "_DB_NAME");
    string db_user = get_config_value(config_path, prefix + "_DB_USER");
    string db_pass = get_config_value(config_path, prefix + "_DB_PASS");

    db_conn.connect(db_name, db_user, db_pass);

    return db_conn;
}

Then you just call get_connection() with your db alias as the argument.

Ryan Ahearn
good thought, but some of the applications in use aren't mine and i cant control how they generate the connection string (ie MS Reporting Services). i just updated my question to reflect this.
Kevin McKinley
yeah, this will only work when you control the code. good luck finding your answer, post it back here if/when you do.
Ryan Ahearn