加入收藏 | 设为首页 | 会员中心 | 我要投稿 东莞站长网 (https://www.0769zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

sql-server – 将SQL Server 2012备份还原到SQL Server 2008数据

发布时间:2021-03-14 22:03:55 所属栏目:MsSql教程 来源:网络整理
导读:有没有办法将SQL Server 2012数据库备份还原到SQL Server 2008? 我试图附加文件,它不起作用. 解决方法 你有几个选择: 选项A:使用“生成脚本”选项在兼容模式下编写数据库: 注意:如果使用模式和数据编写数据库脚本,则根据您的数据大小,脚本将非常庞大,并

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

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

解决方法

你有几个选择:

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

注意:如果使用模式和数据编写数据库脚本,则根据您的数据大小,脚本将非常庞大,并且不会由SSMS,sqlcmd或osql处理(也可能在GB中).

选项B:

首先使用所有索引,FK等脚本输出表,并在目标数据库中创建空白表 – 仅使用SCHEMA(无数据)选项.

使用BCP插入数据

>使用以下脚本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 FilesMicrosoft SQL Server100ToolsBinnbcp.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 -SServerNameInstance -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)

>运行bat文件,该文件将在您指定的文件夹中生成.dat文件.
>再次在文本模式下使用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),'') + ' 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)

>使用SSMS运行输出以将数据插回表中.

这是非常快速的bcp方法,因为它使用Native模式.

(编辑:东莞站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读