4

实际情况:我有一个重复的数据库,想知道哪些表中的数据没有从原始数据库中移动。然后我想从 Original 填充 Duplicate 数据库表。

我编写了以下脚本,实现了部分解决方案(即查找空表):

SELECT
    DBDupl.[dbo].sysobjects.Name, DBDupl.[dbo].sysindexes.Rows FROM
    DBDupl.[dbo].sysobjects
    INNER JOIN DBDupl.[dbo].sysindexes
    ON DBDupl.[dbo].sysobjects.id = DCT_SOURCE_QA.[dbo].sysindexes.id WHERE
    type = 'U'
    AND DBDupl.[dbo].sysindexes.IndId < 2 and rows= '0'
     EXCEPT

SELECT
    DBOrig.[dbo].sysobjects.Name, DBOrig.[dbo].sysindexes.Rows FROM
    DBOrig.[dbo].sysobjects
    INNER JOIN DBOrig.[dbo].sysindexes

    ON DBOrig.[dbo].sysobjects.id = DBOrig.[dbo].sysindexes.id WHERE
    type = 'U'
    AND DBOrig.[dbo].sysindexes.IndId < 2 and rows= '0'

现在我想在空表中填充数据。是否有一个查询来做这两个事情,即(1)。找出哪些表需要填充和(2)。将数据从 DBOrig 填充到 DBDupl。我已经用上面的脚本实现了(1)并且不想手动插入数据。

4

2 回答 2

0

你想要一个精确的数据库副本吗?为什么不备份主要的并使用不同的名称进行恢复?

如果不可能,您可以使用 SSIS 移动数据或类似的东西:

insert into destDB.dbo.mytable
select * sourcedb.dbo.mytable

您可以使用脚本遍历空表并运行一些动态 SQL 来执行上面的命令

于 2013-07-19T12:13:36.310 回答
0

你能不能:

1)获取表格列表

2) 对于这些表中的每一个,从一个数据库插入到另一个

仅供参考 - 如果您需要比较数据库,Visual Studio 可以进行架构/数据比较,而且它是免费的

这是一个示例脚本。它假设表结构是相同的。您可以使用 print 生成脚本或执行命令(这已被注释掉)


SET NOCOUNT ON 


DECLARE
    @objectName nvarchar(256)
    ,@sql nvarchar(max)
    ,@db1 nvarchar(100) = 'DB_1'
    ,@db2 nvarchar(100) = 'DB_2'

DROP TABLE IF EXISTS #EmptyTables


;WITH c_EmptyTables AS (
    SELECT t.object_id
        ,s.Name AS SchemaName
        ,t.Name AS TableName
        ,p.rows AS RowCounts
        ,FullName = CONCAT (
            QUOTENAME(s.name)
            ,'.'
            ,QUOTENAME(t.name)
            )
        ,CAST(ROUND((SUM(a.used_pages) / 128.00), 2) AS NUMERIC(36, 2)) AS Used_MB
        ,CAST(ROUND((SUM(a.total_pages) - SUM(a.used_pages)) / 128.00, 2) AS NUMERIC(36, 2)) AS Unused_MB
        ,CAST(ROUND((SUM(a.total_pages) / 128.00), 2) AS NUMERIC(36, 2)) AS Total_MB
    FROM sys.tables t
    INNER JOIN sys.indexes i ON t.OBJECT_ID = i.object_id
    INNER JOIN sys.partitions p ON i.object_id = p.OBJECT_ID
        AND i.index_id = p.index_id
    INNER JOIN sys.allocation_units a ON p.partition_id = a.container_id
    INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
    WHERE ISNULL(p.rows, 0) = 0
    GROUP BY t.Name
        ,s.Name
        ,p.Rows
        ,t.object_id
)

SELECT *
INTO #EmptyTables
FROM c_EmptyTables

DECLARE cur CURSOR LOCAL FAST_FORWARD
FOR
    SELECT FullName
    FROM #EmptyTables t

OPEN cur 

FETCH NEXT 
FROM cur
INTO 
    @objectName

WHILE @@FETCH_STATUS = 0
BEGIN
    SET @sql = 'INSERT INTO ' + @db1 + '.' + @objectName + CHAR(10)
                + 'SELECT * FROM ' + @db2 + '.' + @objectName
                + CHAR(10) + 'GO' + CHAR(10)

    PRINT(@sql)

    BEGIN TRY
        BEGIN TRANSACTION

            --EXEC(@sql)

        COMMIT TRANSACTION;
    END TRY
    BEGIN CATCH

        IF (@@TRANCOUNT > 0)
            ROLLBACK TRANSACTION;

        PRINT CHAR(10) + '*** UPDATE FAILED FOR ***';
        PRINT CHAR(10) + @sql
        PRINT CHAR(10)

        SELECT
             ERROR_NUMBER() AS ErrorNumber
            ,ERROR_SEVERITY() AS ErrorSeverity
            ,ERROR_STATE() AS ErrorState
            ,ERROR_PROCEDURE() AS ErrorProcedure
            ,ERROR_LINE() AS ErrorLine
            ,ERROR_MESSAGE() AS ErrorMessage;
    END CATCH


    FETCH NEXT 
    FROM cur
    INTO 
        @objectName
END
CLOSE cur
DEALLOCATE cur
于 2019-09-15T13:58:39.323 回答