使用脚本为不同数据库中的所有表创建同义词

发布于 2024-09-15 15:57:59 字数 162 浏览 9 评论 0原文

有没有一种简单的方法可以为不同数据库中的所有表创建同义词?

谢谢

编辑: 我有许多存储过程将一些表模式硬编码到选择查询中。当我将架构复制到新服务器时,SP 会失败,因为架构不存在。我对目标服务器几乎没有控制权,而且我不想更改所有 SP,所以我认为同义词可能是一个很好的解决方案。

Is there an easy way to create synonyms to all tables in a different database?

thanks

EDIT:
I have a number of stored procedures that hardcoded some table schemas into the select queries. When I copy the schemas to a new server, the SPs fail because the schema doesn't exist. There is little control I have over the destination server and I don't want to having to change all the SP, so I thought synonym may be a good solution.

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

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

发布评论

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

评论(3

只怪假的太真实 2024-09-22 15:58:00

另一个数据库是否位于单独的服务器(或实例)上,或者该实例是否包含这两个数据库?

如果这是单个实例,并且两个数据库都位于其上 - 使用三个名称表示法:

SELECT *
  FROM database1.dbo.table_X
  JOIN database2.dbo.table_Y ...

如果另一个数据库存在于单独的 SQL Server 实例上(您可以拥有多个一个机器上有一个 SQL Server),或者数据库存在于不同机器/VM 上的 SQL Server 实例上 - 创建一个 链接服务器实例。然后使用四个名称表示法:

SELECT *
  FROM database1.dbo.table_X
  JOIN linked_server_name.database2.dbo.table_Y

Is the other database on a separate server (or instance), or does the instance hold both databases?

If this is a single instance, and both databases are on it - use three name notation:

SELECT *
  FROM database1.dbo.table_X
  JOIN database2.dbo.table_Y ...

If the other database exists on a separate SQL Server instance (you can have more than one SQL Server on a box), or the database exists on a SQL Server instance on a different box/VM - create a Linked Server instance. Then you use four name notation:

SELECT *
  FROM database1.dbo.table_X
  JOIN linked_server_name.database2.dbo.table_Y
倾城花音 2024-09-22 15:57:59

您可以在原始数据库上运行这样的查询,然后在新数据库上运行输出结果。

select 'create synonym syn_' + t.name + ' for [' + DB_NAME() + '].[' + s.name + '].[' + t.name + ']' 
    from sys.tables t
        inner join sys.schemas s
            on t.schema_id = s.schema_id
    where t.type = 'U'

例如,针对 Master 数据库运行此命令将生成:

create synonym syn_spt_fallback_db for [master].[dbo].[spt_fallback_db]
create synonym syn_spt_fallback_dev for [master].[dbo].[spt_fallback_dev]
create synonym syn_spt_fallback_usg for [master].[dbo].[spt_fallback_usg]
create synonym syn_spt_monitor for [master].[dbo].[spt_monitor]
create synonym syn_spt_values for [master].[dbo].[spt_values]
create synonym syn_MSreplication_options for [master].[dbo].[MSreplication_options]

You could run a query like this on the original database, then run the output results on your new database.

select 'create synonym syn_' + t.name + ' for [' + DB_NAME() + '].[' + s.name + '].[' + t.name + ']' 
    from sys.tables t
        inner join sys.schemas s
            on t.schema_id = s.schema_id
    where t.type = 'U'

As an example, running this against the Master database would produce:

create synonym syn_spt_fallback_db for [master].[dbo].[spt_fallback_db]
create synonym syn_spt_fallback_dev for [master].[dbo].[spt_fallback_dev]
create synonym syn_spt_fallback_usg for [master].[dbo].[spt_fallback_usg]
create synonym syn_spt_monitor for [master].[dbo].[spt_monitor]
create synonym syn_spt_values for [master].[dbo].[spt_values]
create synonym syn_MSreplication_options for [master].[dbo].[MSreplication_options]
李不 2024-09-22 15:57:59

创建一个像这样的存储过程:

CREATE PROCEDURE SynonymUpdate
   @Database nvarchar(256), -- such as 'linkedserver.database' or just 'database'
   @Schema sysname -- such as 'dbo'
AS
CREATE TABLE #Tables (
   TableID int identity(1,1) NOT NULL PRIMARY KEY CLUSTERED,
   Table_Name sysname
)
DECLARE
   @SQL nvarchar(4000),
   @ID int
SET @SQL = N'SELECT Table_Name FROM ' + @Database + '.INFORMATION_SCHEMA.TABLES WHERE Table_Schema = @TableSchema'
INSERT #Tables EXEC sp_executesql @SQL, N'@TableSchema sysname', @Schema
SELECT @ID = MAX(TableID) FROM #Tables
WHILE @ID > 0 BEGIN
   SELECT @SQL = 'CREATE SYNONYM ' + Table_Name + ' FOR ' + @Database + '.' + @Schema + '.' + Table_Name FROM #Tables WHERE TableID = @ID
   PRINT @SQL
   --EXEC sp_executesql @SQL
   SET @ID = @ID - 1
END

然后像这样运行它:

EXEC SynonymUpdate 'Database' , 'dbo'

请注意,您必须以具有创建同义词权限的用户身份运行它。如果您希望没有这些权限的用户运行它,在 SQL 2000 中不行,在 SQL 2005 中您可以在其中放置一个 EXECUTE AS 子句。

Create a stored procedure something like this:

CREATE PROCEDURE SynonymUpdate
   @Database nvarchar(256), -- such as 'linkedserver.database' or just 'database'
   @Schema sysname -- such as 'dbo'
AS
CREATE TABLE #Tables (
   TableID int identity(1,1) NOT NULL PRIMARY KEY CLUSTERED,
   Table_Name sysname
)
DECLARE
   @SQL nvarchar(4000),
   @ID int
SET @SQL = N'SELECT Table_Name FROM ' + @Database + '.INFORMATION_SCHEMA.TABLES WHERE Table_Schema = @TableSchema'
INSERT #Tables EXEC sp_executesql @SQL, N'@TableSchema sysname', @Schema
SELECT @ID = MAX(TableID) FROM #Tables
WHILE @ID > 0 BEGIN
   SELECT @SQL = 'CREATE SYNONYM ' + Table_Name + ' FOR ' + @Database + '.' + @Schema + '.' + Table_Name FROM #Tables WHERE TableID = @ID
   PRINT @SQL
   --EXEC sp_executesql @SQL
   SET @ID = @ID - 1
END

Then run it like this:

EXEC SynonymUpdate 'Database' , 'dbo'

Note that you have to run this as a user with the privilege of creating synonyms. If you want a user without those privileges to run it, in SQL 2000 no luck, in SQL 2005 you can put an EXECUTE AS clause in there.

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