如何创建 Sql 同义词或“别名” 数据库名称?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 3 部分符号和别名直至表,例如
use 3 part notation and alias up to the table, example
有一种方法可以使用链接服务器来模拟这一点。 这假设您有两台具有相同数据库集的 SQL 服务器,一台用于开发/测试,一台用于运行。
以上适用于 SQL Server 2005,但应该是与 2008 年类似
一旦完成此操作,您可以像这样编写 SQL:
现在,当您的脚本在开发服务器上运行且链接服务器返回自身时,它们将正确地从开发服务器提取数据,并且当完全相同的脚本被执行时,它们将正常工作。在实时服务器上运行它们将正常工作。
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.
The above is for SQL Server 2005 but should be similar for 2008
Once you've done that you can write SQL like this:
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.
我使用另一个配置文件做了类似的事情。
新的配置文件将您的通用名称映射到连接到该数据库所需的所有信息(数据库名称、用户名、密码等),然后您的连接函数将您的通用名称作为参数。
db.config:
连接代码:
然后您只需使用数据库别名作为参数调用 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:
connection code:
Then you just call get_connection() with your db alias as the argument.
我知道这可能不会在所有情况下都有帮助,但您仍然可以选择使用视图。 只要视图具有正确的标识键(主键),您就可以对视图进行插入、删除、更新、选择。 如果您将其指向另一个数据库,则应该删除并重新创建以获得不同的架构(如果您在生产和测试之间工作,同时在测试和/或生产中更改架构)。
同义词在您转到另一个数据库并拥有 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.