如何在SQL Server中创建数据库的别名

编程入门 行业动态 更新时间:2024-10-14 14:20:59
本文介绍了如何在SQL Server中创建数据库的别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我们有一个非常古老的软件大约在10年前创建,我们没有源代码。

该软件使用两个数据库, DB01 和 DB02 在同一SQL Server 2012实例上。

SQL语句如 db01..table1 join db02..table2 ,但主要的问题是我们的进程不允许使用 db02 作为数据库的名称。

问题是:我们如何创建数据库的别名?

我试图使用 CREATE SYNONYM

CREATE SYNONYM [db02] FOR [db02_new_name];

但不适用于数据库名称。

$ b $

解决方案

建议如何在不修补二进制文件的情况下解决这个问题。您想要模拟的名称。重新启动DDL代码生成器为数据库中的每个表创建一个视图,该表具有需要通过硬编码名称访问的表。基本上,每个视图将有一个如下所示的语句。

CREATE VIEW schemaname.tablename为SELECT * FROM targetdbname.schemaname .tablename

示例:

目标硬编码的数据库名称称为 ProdDBV1 ,而您拥有的源数据库名为 ProductDatabaseDatabaseV1 ,模式为 dbo 且表名为 customer

  • 使用SSMS或脚本创建名为 ProdDBV1 的数据库。
  • CREATE VIEW dbo.customer as SELECT * FROM ProductDatabaseDatabaseV1 .dbo.customer
  • 如果您可以枚举源数据库中的每个表, DDL如上。如果你想我可以更新此发布与代码示例。 (如果可能,使用 sp_msforeachtable 过程)

    We have a very old software has been created around 10 years ago and we don't have source code.

    The software uses two databases, DB01 and DB02 on the same SQL Server 2012 instance.

    There is SQL statements such as db01..table1 join db02..table2, but the main issue is our processes don't allow us use db02 as a name of database.

    The question is: how we can create an alias of for database?

    I was trying to use CREATE SYNONYM

    CREATE SYNONYM [db02] FOR [db02_new_name];

    but it doesn't work for database names.

    Please suggest how it can be solved without patching a binary files to correct SQL statements.

    解决方案

    Create a database with the name you want to impersonate. Re-jigg the DDL code generator to create a view for every table in the database that has the tables I need to access via the hardcoded name. Basically, each view will have a statement that looks like this..

    CREATE VIEW schemaname.tablename as SELECT * FROM targetdbname.schemaname.tablename

    Example:

    The target database name that is hardcoded is called ProdDBV1 and the Source DB you have is named ProductDatabaseDatabaseV1, schema is dbo and table name is customer

  • Create the database called ProdDBV1 using SSMS or script.
  • CREATE VIEW dbo.customer as SELECT * FROM ProductDatabaseDatabaseV1.dbo.customer
  • If you can enumerate each table in your "source" database and then create the DDL as above. If you want I can update this posting with a code example. (using the sp_msforeachtable procedure if possible)

    更多推荐

    如何在SQL Server中创建数据库的别名

    本文发布于:2023-11-02 03:57:34,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1551303.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:别名   数据库   如何在   Server   SQL

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!