我有一个 830mb 的.sql文件,它是一个脚本数据库。
我希望执行此 SQL,以便它将创建数据并将数据插入到托管服务器上的数据库中。我无法在 SQL Server Management Studio 或 EMS SQL Manager 中打开它。
我怎样才能执行它?
小智 8
取决于 db 风格,大多数提供命令行工具来加载 sql 文件。看起来您可能正在使用 ms sql,请参考此处http://technet.microsoft.com/en-us/library/ms165702(v=sql.105).aspx,更具体的http://technet.microsoft.com/ en-us/library/ms170572.aspx
sqlcmd -S myServer\instanceName -i C:\myScript.sql
Run Code Online (Sandbox Code Playgroud)
SQLCMD 是一个可行的选择,但对于如此庞大的脚本,我从未测试过以确认它是否真的能够处理它。另外,我建议-o logfile.txt如果出现问题,您至少会有一个日志文件来查看出了什么问题。
以下是我的建议:
我有一个 830mb .sql 文件,它是一个脚本数据库。
我想,您已经编写了整个数据库以及架构和数据的脚本,因此具有如此庞大的规模。
我希望执行此 SQL,以便它将创建数据并将数据插入到托管服务器上的数据库中。我无法在 SQL Server Management Studio 或 EMS SQL Manager 中打开它。
您将无法从 SSMS 或任何 GUI 工具运行它。
我怎样才能执行它?
永远不要用数据和模式编写整个数据库的脚本。反而 -
下面是帮助您完成第 2 部分的脚本。
/************************************************************************************************************************************************
Author : KIN SHAH *********************************************************************************************************************
Purpose : Move data from one server to another*********************************************************************************************
DATE : 05-28-2013 *********************************************************************************************************************
Version : 1.0.0 *************************************************************************************************************************
RDBMS : MS SQL Server 2008R2 and 2012 *************************************************************************************************
*************************************************************************************************************************************************/
-- 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_OUT\*.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_OUT\' -- 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) <> 'some_schema_exclude' -- Optional to exclude any schema
order by schema_name(schema_id)
--- 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 = 'destination_database_Name' -- 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_OUT\' -- 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) <> 'some_schema_exclude' -- Optional to exclude any schema
order by schema_name(schema_id)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
39333 次 |
| 最近记录: |