服务器之家:专注于服务器技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|数据库技术|

服务器之家 - 数据库 - Sql Server - SQL Server数据库bcp导出备份文件应用示例

SQL Server数据库bcp导出备份文件应用示例

2020-04-02 15:34whsnow Sql Server

本节主要介绍了SQL Server数据库bcp导出备份文件应用,需要的朋友可以参考下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
* 授权
*/
EXEC sp_configure 'show advanced options',1;
go
reconfigure;
go
exec sp_configure 'xp_cmdshell',1;
go
reconfigure;
go
 
/**导入指定表的文本文件*/
EXEC master..xp_cmdshell 'bcp dbname..tablename in d:\DT.txt -c -Sservername -Usa -Ppassword'
exec master..xp_cmdshell 'bcp "select * from dbname..tablename" queryout "D:\20140528.xls"-c -Sservername -Uuser -Ppassword'

xp_cmdshell参数说明

SQL Server数据库bcp导出备份文件应用示例

下面是我自己写的一个存储过程,可以直接拿去使用
第一步,先要授权。上面有授权的SQL代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
if exists(select * from sysobjects where type='p' and name='sp_export_posm_data') begin
drop procedure sp_export_posm_data;
end;
go
 
create procedure sp_export_posm_data
@file_path varchar(200) /*导出后文件存放的路径*/
as
declare @exec_sql varchar(1000);
declare @file_name varchar(200); /*文件名称,时间格式,主要是用于记录数据是什么时候导出备份的*/
declare @table_name varchar(100); /*要导出数据的表名*/
declare @sql varchar(1000); /*执行业务数据查询的sql语句*/
/*要备份数据的业务表名*/
declare cur_tables cursor for
select name from sysobjects where 1=1 and type='u'
and name like 'WM_ORDER%' or name like 'WM_PICKING%' or name like 'RP_%'
begin try
open cur_tables;
fetch next from cur_tables into @table_name;
while @@FETCH_STATUS = 0 begin
set @file_name = '';
set @file_path = '';
set @sql = 'select * from DHL_POSM_WS..'+@table_name;
set @sql += ' where 1=1 and DATEDIFF(MONTH,MODIFY_TIME,GETDATE())>10';
print @sql;
set @exec_sql = ' bcp "'+@sql+'" queryout ';
if ''=@file_path begin
set @file_path = 'D:\Program Files (x86)\Microsoft SQL Server\';
end;
print '111111';
set @file_name = @table_name+'_'+CONVERT(varchar(100), GETDATE(), 112)+'.xls';
set @file_path = @file_path + @file_name; /*文件路径*/
print '2222222';
set @exec_sql = @exec_sql +'"'+@file_path+'"';
set @exec_sql = @exec_sql +' -c -S"127.0.0.1\SQLEXPRESS" -U"DHL_POSM_WS" -P"DHLposm"';
print @exec_sql;
-- 导出数据到本地文件
exec master..xp_cmdshell @exec_sql;
fetch next from cur_tables into @table_name;
end;
close cur_tables; -- 关闭游标
deallocate cur_tables;-- 释放游标
end try
begin catch
close cur_tables; -- 关闭游标
deallocate cur_tables;-- 释放游标
end catch;
go
 
-- 执行存储过程,进行测试
exec sp_export_posm_data '';

注意事项:

1、查询语句的语法 select * from [数据库名]..[表名];
如果运行过程中出现了SQLState = S1000, NativeError=0这个错误,这表示是你的数据库名或表名写错了
2、bcp 'sql语句' queryout -c -S'IP\数据库服务实例' -U'数据库登录用户名' -P'数据库登录密码'
如果运行过程中出现了SQLState = S0002, NativeError=208这个错误,则表示是你的 -S服务名写错了,
一般常写错是因为 没有加 数据库服务实例,这个可以参考你数据库的连接,照着数据库连接写就可以。
下图是我本地的数据库连接,所以我在写 -S的时候,可以两种写法:-S'127.0.0.1\SQLEXPRESS' 或者 -S'PED-VICKY-251\SQLEXPRESS'

SQL Server数据库bcp导出备份文件应用示例

3、导出文件中文乱码,解决方法
bcp 'sql语句' queryout -c -S'IP\数据库服务实例' -U'数据库登录用户名' -P'数据库登录密码' 改成
bcp 'sql语句' queryout -w -S'IP\数据库服务实例' -U'数据库登录用户名' -P'数据库登录密码'
即 -c 改成 -w 就行

4、导出后的文件存放目录,一定要是SQL Server数据库安装的目录,不然会出错

延伸 · 阅读

精彩推荐