How to create an alias of database in SQL Server

前端 未结 7 1092
鱼传尺愫
鱼传尺愫 2020-12-09 15:12

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

7条回答
  •  醉梦人生
    2020-12-09 16:05

    Here's a stored proc to do it. Simply add it to your database and call it with the target database. It will create synonyms for all tables in the target database, and create the schemas if they don't exist. I've left a commented out section in case someone knows of a way to get the create schemas working without a cursor.

    CREATE PROCEDURE CreateSynonymsForTargetDatabase (
        @databaseName sysname
    )
    AS BEGIN
    DECLARE @TSQL nvarchar(max) = N''
    DECLARE @rn char(2),
        @SchemaName sysname;
    
        SET @rn = char(13) + char(10)   
    
        CREATE TABLE #DBSynonym(        
            [Schema] sysname NOT NULL,
            [Table] sysname NOT NULL
        )
    
        SET @TSQL = N'
            INSERT INTO #DBSynonym ([Schema], [Table])
            SELECT Schemas.name, Tables.name
            FROM [' + @databaseName + '].sys.tables 
            INNER JOIN [' + @databaseName + '].sys.schemas on tables.schema_id = schemas.schema_id      
        '
    
        EXEC (@TSQL)
        SET @TSQL = N''
    
        DECLARE MissingSchemasCursor CURSOR
        READ_ONLY
        FOR 
            SELECT newSchemas.[Schema]
            FROM #DBSynonym newSchemas
            LEFT JOIN sys.schemas on newSchemas.[Schema] = schemas.name
            WHERE schemas.schema_id is null
            GROUP BY newSchemas.[Schema]
    
        OPEN MissingSchemasCursor
        FETCH NEXT FROM MissingSchemasCursor INTO @SchemaName
        WHILE (@@fetch_status <> -1)
        BEGIN
            IF (@@fetch_status <> -2)
            BEGIN
                SET @TSQL = N'CREATE SCHEMA ' + QUOTENAME(@SchemaName) + N';'
    
                EXEC sp_executesql @TSQL
            END
            FETCH NEXT FROM MissingSchemasCursor INTO @SchemaName
        END
        CLOSE MissingSchemasCursor
        DEALLOCATE MissingSchemasCursor
    
        /*
        SELECT @TSQL = @TSQL +
            N'
            GO
            CREATE SCHEMA ' + QUOTENAME([Schema]) + N';'
        FROM #DBSynonym newSchemas
        LEFT JOIN sys.schemas on newSchemas.[Schema] = schemas.name
        WHERE schemas.schema_id is null
        GROUP BY newSchemas.[Schema]
    
        PRINT 'CREATE SCHEMAS : ' + ISNULL(@TSQL,'')
        EXEC sp_executesql @TSQL
        */
        SET @TSQL = N''
    
        SELECT @TSQL = @TSQL +
            N'
            CREATE SYNONYM ' + QUOTENAME([Schema]) + N'.' + QUOTENAME([Table]) + N'
            FOR ' + QUOTENAME(@databaseName) + N'.' + QUOTENAME([Schema]) + N'.' + QUOTENAME([Table]) + N';'
        FROM #DBSynonym
    
    
        EXEC sp_executesql @TSQL
        SET @TSQL = N''
    
    END
    GO
    

    Use it as follows :

    EXEC CreateSynonymsForTargetDatabase 'targetDbName'
    

提交回复
热议问题