我们有一个数据库,我们有大约100个同义词,如下所示:
IF EXISTS (SELECT 1 FROM sys.objects Where type = 'SN' and name = 'CRM_ActivityPointerBase')
DROP SYNONYM CRM_ActivityPointerBase
CREATE SYNONYM CRM_ActivityPointerBase FOR CRMPRODDB.TRAINING_MSCRM.dbo.ActivityPointerBase
GO
IF EXISTS (SELECT 1 FROM sys.objects Where type = 'SN' and name = 'CRM_AnnotationBase')
DROP SYNONYM CRM_AnnotationBase
CREATE SYNONYM CRM_AnnotationBase FOR CRMPRODDB.TRAINING_MSCRM.dbo.AnnotationBase
GO
IF EXISTS (SELECT 1 FROM sys.objects Where type = 'SN' and name = 'CRM_SalesorderBase')
DROP SYNONYM CRM_SalesOrderBase
CREATE SYNONYM CRM_SalesOrderBase FOR CRMPRODDB.TRAINING_MSCRM.dbo.SalesOrderBase
GO
IF EXISTS (SELECT 1 FROM sys.objects Where type = 'SN' and name = 'CRM_BusinessUnitBase')
DROP SYNONYM CRM_BusinessunitBase
CREATE SYNONYM CRM_BusinessUnitBase FOR CRMPRODDB.TRAINING_MSCRM.dbo.BusinessUnitBase
GO
IF EXISTS (SELECT 1 FROM sys.objects Where type = 'SN' and name = 'CRM_SalesorderExtensionBase')
DROP SYNONYM CRM_SalesOrderExtensionBase
CREATE SYNONYM CRM_SalesOrderExtensionBase FOR CRMPRODDB.TRAINING_MSCRM.dbo.SalesOrderExtensionBase
GO
有时我需要所有同义词指向CRMPRODDB.TRAINING_MSCRM,有时我需要所有同义词指向ANOTHERSERVER.myDATABASE。
我的想法是创建一个我只运行的函数,它只会更新每个同义词。
有人可以帮助我开始吗?
我想到这样的事情:
declare @myvar varchar
select @myvar ='CRMPRODDB.TRAINING_MSCRM'
IF EXISTS (SELECT 1 FROM sys.objects Where type = 'SN' and name = 'CRM_ActivityPointerBase')
DROP SYNONYM CRM_ActivityPointerBase
EXECUTE ('CREATE SYNONYM CRM_ActivityPointerBase FOR ' + @myvar);
但我真的很喜欢你对标准做法的指导。
非常感谢你的帮助。
答案 0 :(得分:1)
我会看database projects,它们设置起来有点麻烦,但可以做到这一点,你的定义看起来像这样:
CREATE SYNONYM CRM_SalesOrderExtensionBase FOR $(server).$(database).dbo.SalesOrderExtensionBase
并选择发布配置文件来替换变量。 它们只是SQLCMD脚本,你可以在没有数据库项目的情况下做同样的事情。