将所有同义词更改为另一个数

时间:2013-10-02 08:10:45

标签: sql sql-server sql-server-2008

我有生产数据库(例如,PROD1)。它与其他数据库有很多同义词(例如,PROD2)。

我创建了PROD2_TEST(作为PROD2的完整副本)和PROD1_TEST(作为PROD1的完整副本)。但结果是PROD1_TEST代表了PROD2,而不是PROD2_TEST

如何自动重新创建测试数据库的所有同义词?

4 个答案:

答案 0 :(得分:6)

基于@artm建议我将它扩展了一点,以包含参数以使其更通用。这是代码

DECLARE @newDB VARCHAR(MAX) = 'newDB',  --newDB to point the synonym to
    @linkedSrv VARCHAR(MAX) = null --if the synonym points to a linked server than specify it here like '[LinkedSrv].' (including the dot in the end)

SELECT  'Drop Synonym [' + SCHEMA_NAME(schema_id) + '].[' + PARSENAME(base_object_name, 1) + '];' AS dropSynonym,
        ' CREATE SYNONYM [' + SCHEMA_NAME(schema_id) + '].[' + UPPER(PARSENAME(base_object_name, 1)) + '] FOR '
        + COALESCE(@linkedSrv, '') + '[' + @newDB + '].[' + COALESCE(PARSENAME(base_object_name, 2),
                                                                     SCHEMA_NAME(SCHEMA_ID())) + '].['
        + UPPER(PARSENAME(base_object_name, 1)) + '];' AS createSynonym
FROM    sys.synonyms`

答案 1 :(得分:5)

出于同样的原因,我必须这样做,这是我使用的查询。运行查询,将结果复制粘贴到新服务器(PROD_Test)并运行生成的查询。它处理不同的模式名称和多个数据库,以防您使用它们:

SELECT 'Drop Synonym [' + SCHEMA_NAME(schema_id) + '].[' + PARSENAME(base_object_name,1) + '];CREATE SYNONYM [' + 
    SCHEMA_NAME(schema_id) + '].[' + PARSENAME(base_object_name,1) + '] FOR [' + COALESCE(PARSENAME(base_object_name,3),DB_NAME(DB_ID())) + '_Test].[' 
    + COALESCE(PARSENAME(base_object_name,2),SCHEMA_NAME(SCHEMA_ID())) + '].[' + PARSENAME(base_object_name,1) + '];'
FROM sys.synonyms

答案 2 :(得分:1)

无法更改同义词,您必须删除并创建它们。以下文章包含用于为一系列表创建同义词的脚本。希望您可以根据自己的需要进行修改:

Use synonyms to abstract SQL Server objects

您可以使用sys.synonyms视图查看所有现有同义词。在伪代码中,您要做的是:

  • 创建一个临时表来存储同义词namebase_object_name
  • 从sys.synonyms中选择所有不正确的同义词到临时表
  • 删除不正确的同义词
  • 构建语句以复制同义词,但将PROD2更改为PROD2_TEST
  • 运行该声明

答案 3 :(得分:1)

以下是我用来将所有同义词重新映射到同一服务器上的新数据库的内容(基于Tobsey的建议)

SET NOCOUNT ON;

DECLARE @db_name SYSNAME
DECLARE @schema_name SYSNAME
DECLARE @cmd NVARCHAR(256)

SET @schema_name = N'dbo'
SET @db_name = N'[newdb]'

DECLARE @schema_id INT

SELECT @schema_id = schema_id
FROM sys.schemas
WHERE NAME = @schema_name;

DECLARE @table_list TABLE (table_name SYSNAME,dest_table_name sysname,dest_schema_name sysname)
DECLARE @table_name SYSNAME
declare @dest_table_name sysname 
declare @dest_schema_name sysname 
DECLARE @prefix NVARCHAR(50)
DECLARE @synonym SYSNAME
DECLARE @linked_server SYSNAME

SET @linked_server = N'SERVER'


-- create a synonym for these tables (insert each table)
INSERT INTO @table_list (table_name,dest_table_name,dest_schema_name)
SELECT name
,'['+SUBSTRING(name,CHARINDEX('.',name,CHARINDEX('.',name,0)+1)+1,LEN(name)-CHARINDEX('.',name,CHARINDEX('.',name,0)+1))+']'
,SUBSTRING(name,CHARINDEX('.',name,0)+1,CHARINDEX('.',name,CHARINDEX('.',name,0)+1)-CHARINDEX('.',name,0)-1)
FROM sys.synonyms
WHERE base_object_name LIKE '![SERVER!].![olddb!]%' ESCAPE ('!')
ORDER BY name

IF EXISTS (
        SELECT *
        FROM sys.servers
        WHERE NAME = @linked_server
        )
    -- 4 part name
    SET @prefix = @linked_server + N'.' + @db_name 
ELSE
    -- 3 part name
    SET @prefix = @db_name 

SELECT TOP 1 @table_name = table_name,@dest_table_name = dest_table_name,@dest_schema_name=dest_schema_name
FROM @table_list

WHILE @table_name IS NOT NULL
BEGIN
    -- set the schema and name for the synonym
    SET @synonym = @schema_name+ N'.' + '['+@table_name+']'

    -- delete the synonym if it exists
    IF EXISTS (
            SELECT *
            FROM sys.synonyms
            WHERE NAME = @table_name
                AND schema_id = @schema_id
            )
    BEGIN
        SET @cmd = N'drop synonym ' + @synonym
        PRINT @cmd
        --EXEC sp_executesql @cmd
    END

    -- create the synonym
    SET @cmd = N'create synonym ' + @synonym + N' for ' + @prefix + N'.'+ @dest_schema_name + N'.'+ @dest_table_name

    PRINT @cmd
    --EXEC sp_executesql @cmd
    PRINT 'GO'

    DELETE TOP (1)
    FROM @table_list

    SET @table_name = NULL

    SELECT TOP 1 @table_name = table_name,@dest_table_name = dest_table_name,@dest_schema_name=dest_schema_name
    FROM @table_list
END
GO