简体   繁体   English

在具有相同表名的SQL Server 2016中创建同义词时出错

[英]Error when creating Synonym in SQL Server 2016 with the same table name

We have a synonym created for the DB that is year specific ie DST2016. 我们为数据库创建了一个特定年份的同义词,即DST2016。 I want to create synonym for the database for the 2017 year ie DST2017, so that all our jobs point to the year 2017 automatically. 我想为2017年数据库创建同义词,即DST2017,以便我们所有的工作都自动指向2017年。

When I run the command create synonym ABS for [DST2017].[dbo].[ABS] I get the following error: 当我运行命令create synonym ABS for [DST2017].[dbo].[ABS]出现以下错误:

There is already an object named 'ABS' in the database. 数据库中已经有一个名为“ ABS”的对象。

Any idea how I can get around this? 知道如何解决这个问题吗?

I'm not sure where you have created the old ones (synonym), but maybe you could do something like this to remove the old ones: 我不确定您在哪里创建了旧版本(同义词),但是也许您可以执行以下操作删除旧版本:

select 'drop  synonym ' + name 
from sys.tables order by name 

Then use this command to create the new ones: 然后使用此命令创建新的:

declare @db as varchar(50) = (select db_name())
select 'create synonym ' + name + ' for ' + @db + '.dbo.' + name 
from sys.tables order by name 

You also need to be in a different database that the tables are in, otherwise you will get the error above. 您还需要位于表所在的其他数据库中,否则您将收到上面的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM