经常遇到这种情况:需要获得整个远程服务器上的SQLServer数据库,但是没有权限导出数据,而使用脚本生成的方法总有这样那样的问题,并且速度超慢。这两天查找了有关的工具,发现BCP 命令行工具适合大量导入导出,BULK INSERT (SQLServer2005之后支持)命令适合带有自动增长列的表数据导入。
首先要生成数据库的脚本,在本地建立数据库,这个了解SQL Server的基本上都知道。
然后使用下列脚本进行数据的复制了。
Code
1DECLARE @remoteServerNameOrIP VARCHAR(50); --远程服务器名称或IP
2SET @remoteServerNameOrIP = '205.100.100.100';
3DECLARE @remoteServerUserName VARCHAR(50); --远程服务器用户名
4SET @remoteServerUserName = 'user';
5DECLARE @remoteServerPassword VARCHAR(50); --远程服务器密码
6SET @remoteServerPassword = '123';
7DECLARE @remoteServerDatabaseName VARCHAR(50); --远程数据库名
8SET @remoteServerDatabaseName = 'global';
9DECLARE @localServerNameOrIP VARCHAR(50); --本地服务器名称或IP
10SET @localServerNameOrIP = '127.0.0.1';
11DECLARE @localServerDatabaseName VARCHAR(50); --本地数据库名
12SET @localServerDatabaseName = 'global';
13DECLARE @tableName VARCHAR(50); --远程\本地数据库中表名
14DECLARE @fileDir VARCHAR(50); --导出\导入文件目录
15SET @fileDir = 'D:\global\';
16DECLARE @isSimple bit; --是否单表
17SET @isSimple = 1;
18DECLARE @simpleTableName VARCHAR(50);
19SET @simpleTableName = 'NewsImages';
20
21exec('declare #tb cursor for select name from '+@localServerDatabaseName+'..sysobjects where xtype=''U''')
22open #tb
23fetch next from #tb into @tableName
24while @@fetch_status=0
25begin
26 --查看表数目
27 EXEC('SELECT '''+@tableName+''', COUNT(*) FROM '+@localServerDatabaseName+'.dbo.'+@tableName);
28 IF (@isSimple=0 OR (@isSimple=1 AND @tableName=@simpleTableName))
29 BEGIN
30
31 --导出远程数据库表数据到本地文件
32 DECLARE @bcp VARCHAR(500);
33 SET @bcp = 'BCP "'+@remoteServerDatabaseName+'.dbo.'+@tableName+'" out "'+@fileDir+@tableName+'.txt" -w -S"'+@remoteServerNameOrIP+'" -U"'+@remoteServerUserName+'" -P"'+@remoteServerPassword+'"';
34 --EXEC master..xp_cmdshell @bcp;
35
36 --导入数据文件到本地数据库
37 DECLARE @bulk VARCHAR(500);
38 SET @bulk = 'BULK INSERT '+@localServerDatabaseName+'.dbo.'+@tableName+' FROM "'+@fileDir+@tableName+'.txt"
39 WITH(
40 CHECK_CONSTRAINTS,
41 DATAFILETYPE = ''widechar'',
42 KEEPIDENTITY,
43 KEEPNULLS,
44 MAXERRORS = 1000,
45
46 ERRORFILE = '''+@fileDir+@tableName+'_err.txt''
47 )';
48 --EXEC(@bulk);
49 END
50fetch next from #tb into @tableName
51end
52close #tb
53deallocate #tb
1DECLARE @remoteServerNameOrIP VARCHAR(50); --远程服务器名称或IP
2SET @remoteServerNameOrIP = '205.100.100.100';
3DECLARE @remoteServerUserName VARCHAR(50); --远程服务器用户名
4SET @remoteServerUserName = 'user';
5DECLARE @remoteServerPassword VARCHAR(50); --远程服务器密码
6SET @remoteServerPassword = '123';
7DECLARE @remoteServerDatabaseName VARCHAR(50); --远程数据库名
8SET @remoteServerDatabaseName = 'global';
9DECLARE @localServerNameOrIP VARCHAR(50); --本地服务器名称或IP
10SET @localServerNameOrIP = '127.0.0.1';
11DECLARE @localServerDatabaseName VARCHAR(50); --本地数据库名
12SET @localServerDatabaseName = 'global';
13DECLARE @tableName VARCHAR(50); --远程\本地数据库中表名
14DECLARE @fileDir VARCHAR(50); --导出\导入文件目录
15SET @fileDir = 'D:\global\';
16DECLARE @isSimple bit; --是否单表
17SET @isSimple = 1;
18DECLARE @simpleTableName VARCHAR(50);
19SET @simpleTableName = 'NewsImages';
20
21exec('declare #tb cursor for select name from '+@localServerDatabaseName+'..sysobjects where xtype=''U''')
22open #tb
23fetch next from #tb into @tableName
24while @@fetch_status=0
25begin
26 --查看表数目
27 EXEC('SELECT '''+@tableName+''', COUNT(*) FROM '+@localServerDatabaseName+'.dbo.'+@tableName);
28 IF (@isSimple=0 OR (@isSimple=1 AND @tableName=@simpleTableName))
29 BEGIN
30
31 --导出远程数据库表数据到本地文件
32 DECLARE @bcp VARCHAR(500);
33 SET @bcp = 'BCP "'+@remoteServerDatabaseName+'.dbo.'+@tableName+'" out "'+@fileDir+@tableName+'.txt" -w -S"'+@remoteServerNameOrIP+'" -U"'+@remoteServerUserName+'" -P"'+@remoteServerPassword+'"';
34 --EXEC master..xp_cmdshell @bcp;
35
36 --导入数据文件到本地数据库
37 DECLARE @bulk VARCHAR(500);
38 SET @bulk = 'BULK INSERT '+@localServerDatabaseName+'.dbo.'+@tableName+' FROM "'+@fileDir+@tableName+'.txt"
39 WITH(
40 CHECK_CONSTRAINTS,
41 DATAFILETYPE = ''widechar'',
42 KEEPIDENTITY,
43 KEEPNULLS,
44 MAXERRORS = 1000,
45
46 ERRORFILE = '''+@fileDir+@tableName+'_err.txt''
47 )';
48 --EXEC(@bulk);
49 END
50fetch next from #tb into @tableName
51end
52close #tb
53deallocate #tb