将 SQL Server 2012 备份还原到 SQL Server 2008 数据库?

41 sql-server-2008 sql-server sql-server-2012

有没有办法将 SQL Server 2012 数据库备份还原到 SQL Server 2008?

我试图附加文件,它不起作用。

Kin*_*hah 31

你有几个选择:

选项 A:使用生成脚本选项在兼容模式下编写数据库脚本:

注意:如果您使用模式和数据编写数据库脚本,根据您的数据大小,脚本将非常庞大,并且不会被 SSMS、sqlcmd 或 osql(也可能以 GB 为单位)处理。

在此处输入图片说明

选项 B:

首先使用所有索引、FK 等编写表的脚本,然后在目标数据库中创建空白表 - 仅使用 SCHEMA 选项(无数据)。

使用 BCP 插入数据

  1. 使用以下脚本 bcp 输出数据。将 SSMS 设置为文本模式,并将以下脚本生成的输出复制到 bat 文件中。

    -- save below output in a bat file by executing below in SSMS in TEXT mode
    
    -- clean up: create a bat file with this command --> del D:\BCP\*.dat 
    
    select '"C:\Program Files\Microsoft SQL Server\100\Tools\Binn\bcp.exe" ' /* path to BCP.exe */
        +  QUOTENAME(DB_NAME())+ '.' /* Current Database */
        +  QUOTENAME(SCHEMA_NAME(SCHEMA_ID))+'.'            
        +  QUOTENAME(name)  
        +  ' out D:\BCP\'  /* Path where BCP out files will be stored */
        +  REPLACE(SCHEMA_NAME(schema_id),' ','') + '_' 
        +  REPLACE(name,' ','') 
        + '.dat -T -E -SServerName\Instance -n' /* ServerName, -E will take care of Identity, -n is for Native Format */
    from sys.tables
    where is_ms_shipped = 0 and name <> 'sysdiagrams'                       /* sysdiagrams is classified my MS as UserTable and we dont want it */
    /*and schema_name(schema_id) <> 'unwantedschema'    */                             /* Optional to exclude any schema  */
    order by schema_name(schema_id)
    
    Run Code Online (Sandbox Code Playgroud)
  2. 运行将在您指定的文件夹中生成 .dat 文件的 bat 文件。

  3. 再次在文本模式下使用 SSMS 在目标服务器上运行以下脚本。

    --- Execute this on the destination server.database from SSMS.
    
    --- Make sure the change the @Destdbname and the bcp out path as per your environment.
    
    declare @Destdbname sysname
    set @Destdbname = 'destinationDB' /* Destination Database Name where you want to Bulk Insert in */
    select 'BULK INSERT ' 
    /*Remember Tables must be present on destination database */ 
    + QUOTENAME(@Destdbname) + '.' 
    + QUOTENAME(SCHEMA_NAME(SCHEMA_ID)) 
    + '.' + QUOTENAME(name) 
    + ' from ''D:\BCP\' /* Change here for bcp out path */ 
    + REPLACE(SCHEMA_NAME(schema_id), ' ', '') + '_' + REPLACE(name, ' ', '') 
    + '.dat'' with ( KEEPIDENTITY, DATAFILETYPE = ''native'', TABLOCK )' 
    + char(10) 
    + 'print ''Bulk insert for ' + REPLACE(SCHEMA_NAME(schema_id), ' ', '') + '_' + REPLACE(name, ' ', '') + ' is done... ''' 
    + char(10) + 'go'
       from sys.tables
       where is_ms_shipped = 0
    and name <> 'sysdiagrams' /* sysdiagrams is classified my MS as UserTable and we dont want it */
    and schema_name(schema_id) <> 'unwantedschema' /* Optional to exclude any schema */
        order by schema_name(schema_id) 
    
    Run Code Online (Sandbox Code Playgroud)
  4. 使用 SSMS 运行输出以将数据插入回表中。

这是非常快速的 bcp 方法,因为它使用本机模式。


Aar*_*and 23

不,你不能后退,只能前进。您可以在 2008 上创建一个空数据库,然后使用 Management Studio 中的生成脚本向导来编写模式和数据(或来自 Red Gate 和其他公司的 3rd 方比较工具)的脚本。确保您将正确的目标版本设置为 2008,并且您必须充实您在 2012 年可能使用过的不兼容的内容(例如 OFFSET 或 FORMAT)。


小智 8

没有支持的方法来执行此操作,因为 SQL Server 不允许这种兼容性。

你能做的是

  1. 在 SQL 2012 上恢复数据库

  2. 为对象和数据生成脚本

  3. 从 SQL 2012 独有的所有细节中清理脚本
  4. 在 2008 上执行脚本

如果您没有 SQL Server 2012,那么您可以使用第三方工具读取备份并提取数据和结构。

在这种情况下,只需在 SQL 2008 上创建空数据库并使用ApexSQL DiffApexSQL Data Diff等工具来同步对象和数据。您也可以从其他主要供应商处找到这些,例如 Red-Gate 或 Idera。