最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

SQLServer数据库bcp导出备份文件应用

来源:动视网 责编:小采 时间:2020-11-09 13:40:18
文档

SQLServer数据库bcp导出备份文件应用

SQLServer数据库bcp导出备份文件应用:/** * 授权 */ EXEC sp_configure 'show advanced options',1; go reconfigure; go exec sp_configure 'xp_cmdshell',1; go reconfigure; go /**导入指定表的文本文件*/ EXEC master..xp_cmdshell 'bcp dbn
推荐度:
导读SQLServer数据库bcp导出备份文件应用:/** * 授权 */ EXEC sp_configure 'show advanced options',1; go reconfigure; go exec sp_configure 'xp_cmdshell',1; go reconfigure; go /**导入指定表的文本文件*/ EXEC master..xp_cmdshell 'bcp dbn


/** * 授权 */ 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

  /**

  * 授权

  */

  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代码

  if exists(select * from sysobjects where type='p' and) 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.1SQLEXPRESS" -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.1SQLEXPRESS' 或者 -S'PED-VICKY-251SQLEXPRESS'

  3、导出文件中文乱码,解决方法

  bcp 'sql语句' queryout -c -S'IP数据库服务实例' -U'数据库登录用户名' -P'数据库登录密码' 改成

  bcp 'sql语句' queryout -w -S'IP数据库服务实例' -U'数据库登录用户名' -P'数据库登录密码'

  即 -c 改成 -w 就行

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

文档

SQLServer数据库bcp导出备份文件应用

SQLServer数据库bcp导出备份文件应用:/** * 授权 */ EXEC sp_configure 'show advanced options',1; go reconfigure; go exec sp_configure 'xp_cmdshell',1; go reconfigure; go /**导入指定表的文本文件*/ EXEC master..xp_cmdshell 'bcp dbn
推荐度:
标签: 备份 文件 导出
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top