在线等:sql 2005 如何按照一个字段的不同值批量导出多个txt

猪扒界 2013-06-26 09:01:58
一、原表结构如下
月份 编号 属性 单位 部门 加入时间 所属室
201305 1009 1 A A1 201108 研发室
201305 1009 1 B B1 201207 推广室
201305 1009 1 C C1 201301 支撑室
201305 1009 1 D D1 201109 服务室
201305 1013 2 C C2 201302 支撑室
201305 1027 2 A A3 201007 研发室

... ... ... ... ... ... ...
原表比较大,有400万条记录,网上的方法都是导出EXCEL的,能否通过查询方式实现批量导出,谢谢。

二、期待结果
按“所属室”的不同值批量导出TXT文件,呈现如下:


研发室.txt,内容如下:
月份 编号 属性 单位 部门 加入时间 所属室
201305 1009 1 A A1 201108 研发室
201305 1027 2 A A3 201007 研发室

推广室.txt,内容如下:
月份 编号 属性 单位 部门 加入时间 所属室
201305 1009 1 B B1 201207 推广室

支撑室.txt,内容如下:
月份 编号 属性 单位 部门 加入时间 所属室
201305 1009 1 C C1 201301 支撑室
201305 1013 2 C C2 201302 支撑室

服务室.txt,内容如下:
月份 编号 属性 单位 部门 加入时间 所属室
201305 1009 1 D D1 201109 服务室
...全文
377 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
KevinLiu 2013-06-27
  • 打赏
  • 举报
回复
引用 8 楼 bbaizz 的回复:
[quote=引用 6 楼 stublue 的回复:]

EXEC sp_configure 'show advanced options', 1 
GO 
RECONFIGURE 
GO 
EXEC sp_configure 'xp_cmdshell', 1 
GO 
RECONFIGURE 
GO 

if(object_id('testdb.dbo.tbl') is not null) drop table tbl;

select '201305' 月份,1009 编号,1 属性,'A' 单位,'A1' 部门,'201108' 加入时间,N'研发室' 所属室	
into tbl
union all select '201305',1009,1,'B','B1','201207',N'推广室'		
union all select '201305',1009,1,'C','C1','201301',N'支撑室'		
union all select '201305',1009,1,'D','D1','201109',N'服务室'		
union all select '201305',1013,2,'C','C2','201302',N'支撑室'		
union all select '201305',1027,2,'A','A3','201007',N'研发室'

if(object_id('tempdb..#tbWhile') is not null) drop table #tbWhile;
select distinct [所属室]
into #tbWhile
from tbl

declare @str Nvarchar(50)='';
declare @execStr nvarchar(1000)='';

select  top 1 @str=[所属室]
from #tbWhile

while(@str<>'')
begin

truncate table temp_tbl;
insert into temp_tbl
select *
FROM [testdb].[dbo].tbl 
where [所属室]=@str

set @execStr=N'bcp [testdb].[dbo].temp_tbl out "D:\'+ convert(varchar(20),getdate(),112)+replace(convert(varchar(20),getdate(),114),':','')+'.txt" -T -w'
Exec master..xp_cmdshell @execStr

delete from #tbWhile
where [所属室]=@str;
set @str='';
select  top 1 @str=[所属室]
from #tbWhile;
end
以上代码不行呀,我的是2005, 我按2005的改了,提示temp_tbl表不存存或无权限访问。 请大神再帮忙优化下,谢谢啦。[/quote] 人家给的是实例,你需要把表替换成自己的表。错误已经很清楚了。
猪扒界 2013-06-26
  • 打赏
  • 举报
回复
还有高人能够 继续补充吗?
猪扒界 2013-06-26
  • 打赏
  • 举报
回复
引用 2 楼 stublue 的回复:

EXEC sp_configure 'show advanced options', 1 
GO 
RECONFIGURE 
GO 
EXEC sp_configure 'xp_cmdshell', 1 
GO 
RECONFIGURE 
GO 

Exec master..xp_cmdshell 'bcp "SELECT *  FROM [TestDB].[dbo].[Test] where 所属室=''研发室''" queryout "D:\test2.txt" -c -T'

--参考: http://www.cnblogs.com/stublue/archive/2012/07/08/2581300.html
写个循环,每个所属室导出一次就行了
1、循环如何写呀? 2、导出时没有字段名呢?
Leon_He2014 2013-06-26
  • 打赏
  • 举报
回复

EXEC sp_configure 'show advanced options', 1 
GO 
RECONFIGURE 
GO 
EXEC sp_configure 'xp_cmdshell', 1 
GO 
RECONFIGURE 
GO 

Exec master..xp_cmdshell 'bcp "SELECT *  FROM [TestDB].[dbo].[Test] where 所属室=''研发室''" queryout "D:\test2.txt" -c -T'

--参考: http://www.cnblogs.com/stublue/archive/2012/07/08/2581300.html
写个循环,每个所属室导出一次就行了
KevinLiu 2013-06-26
  • 打赏
  • 举报
回复
你可以用SSIS实现
猪扒界 2013-06-26
  • 打赏
  • 举报
回复
引用 6 楼 stublue 的回复:

EXEC sp_configure 'show advanced options', 1 
GO 
RECONFIGURE 
GO 
EXEC sp_configure 'xp_cmdshell', 1 
GO 
RECONFIGURE 
GO 

if(object_id('testdb.dbo.tbl') is not null) drop table tbl;

select '201305' 月份,1009 编号,1 属性,'A' 单位,'A1' 部门,'201108' 加入时间,N'研发室' 所属室	
into tbl
union all select '201305',1009,1,'B','B1','201207',N'推广室'		
union all select '201305',1009,1,'C','C1','201301',N'支撑室'		
union all select '201305',1009,1,'D','D1','201109',N'服务室'		
union all select '201305',1013,2,'C','C2','201302',N'支撑室'		
union all select '201305',1027,2,'A','A3','201007',N'研发室'

if(object_id('tempdb..#tbWhile') is not null) drop table #tbWhile;
select distinct [所属室]
into #tbWhile
from tbl

declare @str Nvarchar(50)='';
declare @execStr nvarchar(1000)='';

select  top 1 @str=[所属室]
from #tbWhile

while(@str<>'')
begin

truncate table temp_tbl;
insert into temp_tbl
select *
FROM [testdb].[dbo].tbl 
where [所属室]=@str

set @execStr=N'bcp [testdb].[dbo].temp_tbl out "D:\'+ convert(varchar(20),getdate(),112)+replace(convert(varchar(20),getdate(),114),':','')+'.txt" -T -w'
Exec master..xp_cmdshell @execStr

delete from #tbWhile
where [所属室]=@str;
set @str='';
select  top 1 @str=[所属室]
from #tbWhile;
end
以上代码不行呀,我的是2005, 我按2005的改了,提示temp_tbl表不存存或无权限访问。 请大神再帮忙优化下,谢谢啦。
猪扒界 2013-06-26
  • 打赏
  • 举报
回复
不行呀,我的是SQL2005
Leon_He2014 2013-06-26
  • 打赏
  • 举报
回复

EXEC sp_configure 'show advanced options', 1 
GO 
RECONFIGURE 
GO 
EXEC sp_configure 'xp_cmdshell', 1 
GO 
RECONFIGURE 
GO 

if(object_id('testdb.dbo.tbl') is not null) drop table tbl;

select '201305' 月份,1009 编号,1 属性,'A' 单位,'A1' 部门,'201108' 加入时间,N'研发室' 所属室	
into tbl
union all select '201305',1009,1,'B','B1','201207',N'推广室'		
union all select '201305',1009,1,'C','C1','201301',N'支撑室'		
union all select '201305',1009,1,'D','D1','201109',N'服务室'		
union all select '201305',1013,2,'C','C2','201302',N'支撑室'		
union all select '201305',1027,2,'A','A3','201007',N'研发室'

if(object_id('tempdb..#tbWhile') is not null) drop table #tbWhile;
select distinct [所属室]
into #tbWhile
from tbl

declare @str Nvarchar(50)='';
declare @execStr nvarchar(1000)='';

select  top 1 @str=[所属室]
from #tbWhile

while(@str<>'')
begin

truncate table temp_tbl;
insert into temp_tbl
select *
FROM [testdb].[dbo].tbl 
where [所属室]=@str

set @execStr=N'bcp [testdb].[dbo].temp_tbl out "D:\'+ convert(varchar(20),getdate(),112)+replace(convert(varchar(20),getdate(),114),':','')+'.txt" -T -w'
Exec master..xp_cmdshell @execStr

delete from #tbWhile
where [所属室]=@str;
set @str='';
select  top 1 @str=[所属室]
from #tbWhile;
end
猪扒界 2013-06-26
  • 打赏
  • 举报
回复
有没有更好的办法呀?

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧