解决方法
你有几个选择:
注意:如果使用模式和数据编写数据库脚本,则根据您的数据大小,脚本将非常庞大,并且不会由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 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)
>运行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运行输出以将数据插回表中.