How to create Sql Synonym or “Alias” for Database Name?

后端 未结 4 1501
隐瞒了意图╮
隐瞒了意图╮ 2020-12-16 10:00

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 re

相关标签:
4条回答
  • 2020-12-16 10:52

    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.

    0 讨论(0)
  • 2020-12-16 10:54

    There is a way to simulate this using a linked server. This assumes you have two SQL servers with the same set of databases one for development/test and one live.

    1. Open SQL Server Management Studio on your development/test server
    2. Right click Server Objects > Linked Servers
    3. Select New Linked Server...
    4. Select the General page
    5. Specify alias name in Linked server field - this would normally be the name of your live server
    6. Select SQL Native Client as the provider
    7. Enter sql_server for Product Name
    8. In Data Source specify the name of the development server
    9. Add Security and Server Options to taste
    10. Click OK

    The above is for SQL Server 2005 but should be similar for 2008

    Once you've done that you can write SQL like this:

    SELECT * FROM liveservername.databasename.dbo.tablename
    

    Now when your scripts are run on the development server with the linked server back to itself they will work correctly pulling data from the development server and when the exact same scripts are run on the live server they will work normally.

    0 讨论(0)
  • 2020-12-16 10:55

    I know this probably will not help in all situations, but you still have the option of using views. You can insert, delete, update, select into a view as long as it has a proper identity key (Primary Key). If you point it to another database, you should drop and recreate to get the different schema (in case you're working between production and test while making changes to the schema in test and/or production.

    Synonyms are useful for when you're going to another database and have a 3 or 4 part name, but when you want to make it so you can have a set name, a linked server will also work which will let you use a fixed name if the table names are the same in both databases and you're just pointing between prod and test.

    0 讨论(0)
  • 2020-12-16 10:59

    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
    
    0 讨论(0)
提交回复
热议问题