如何创建 Sql 同义词或“别名” 数据库名称?

发布于 2024-07-12 00:11:25 字数 306 浏览 6 评论 0原文

我正在使用 ms sql 2008 并尝试创建引用另一个数据库的数据库名称。 例如,“Dev”、“Test”、“Demo”将是我可以从多个配置文件中引用的数据库名称,但每个名称都指向另一个数据库,例如“db20080101”或“db20080114”。

[编辑]一些配置适用于我控制代码的应用程序,有些则不然(例如MS Reporting服务数据源文件配置)[/编辑]

sqlserver似乎只支持视图、表、存储过程或函数的同义词。 别名用于表名和列名。

有没有办法做到这一点,我在文档中错过了? 有人对解决方法有什么建议吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

善良天后 2024-07-19 00:11:25

使用 3 部分符号和别名直至表,例如

select * from tempdb.dbo.sysobjects a
join master.dbo.sysobjects b on a.id = b.id

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
陌上芳菲 2024-07-19 00:11:25

有一种方法可以使用链接服务器来模拟这一点。 这假设您有两台具有相同数据库集的 SQL 服务器,一台用于开发/测试,一台用于运行。

  1. 在您的开发/测试服务器上打开 SQL Server Management Studio,
  2. 右键单击“服务器对象”> 链接服务器
  3. 选择新链接服务器...
  4. 选择常规页面
  5. 在链接服务器字段中指定别名名称 - 这通常是您的实时服务器的名称
  6. 选择 SQL Native Client 作为提供程序
  7. 在产品名称中输入 sql_server
  8. 在数据源中指定开发服务器的名称
  9. 添加安全性和服务器选项以供选择
  10. 单击确定

以上适用于 SQL Server 2005,但应该是与 2008 年类似

一旦完成此操作,您可以像这样编写 SQL:

SELECT * FROM liveservername.databasename.dbo.tablename

现在,当您的脚本在开发服务器上运行且链接服务器返回自身时,它们将正确地从开发服务器提取数据,并且当完全相同的脚本被执行时,它们将正常工作。在实时服务器上运行它们将正常工作。

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.

妖妓 2024-07-19 00:11:25

我使用另一个配置文件做了类似的事情。

新的配置文件将您的通用名称映射到连接到该数据库所需的所有信息(数据库名称、用户名、密码等),然后您的连接函数将您的通用名称作为参数。

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

连接代码:

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;
}

然后您只需使用数据库别名作为参数调用 get_connection() 即可。

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.

独自唱情﹋歌 2024-07-19 00:11:25

我知道这可能不会在所有情况下都有帮助,但您仍然可以选择使用视图。 只要视图具有正确的标识键(主键),您就可以对视图进行插入、删除、更新、选择。 如果您将其指向另一个数据库,则应该删除并重新创建以获得不同的架构(如果您在生产和测试之间工作,同时在测试和/或生产中更改架构)。

同义词在您转到另一个数据库并拥有 3 或 4 部分名称,但是当您想要设置名称时,链接服务器也可以工作,如果表名称在中相同,则链接服务器将允许您使用固定名称两个数据库,您只是在产品和测试之间指向。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文