一、原表结构如下
月份      编号       属性      单位      部门      加入时间    所属室
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    服务室
不同值 批量 导出多个txt

解决方案 »

  1.   


    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写个循环,每个所属室导出一次就行了
      

  2.   


    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 tbldeclare @str Nvarchar(50)='';
    declare @execStr nvarchar(1000)='';select  top 1 @str=[所属室]
    from #tbWhilewhile(@str<>'')
    begintruncate table temp_tbl;
    insert into temp_tbl
    select *
    FROM [testdb].[dbo].tbl 
    where [所属室]=@strset @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 @execStrdelete from #tbWhile
    where [所属室]=@str;
    set @str='';
    select  top 1 @str=[所属室]
    from #tbWhile;
    end
      

  3.   

    以上代码不行呀,我的是2005,
    我按2005的改了,提示temp_tbl表不存存或无权限访问。
    请大神再帮忙优化下,谢谢啦。
      

  4.   

    以上代码不行呀,我的是2005,
    我按2005的改了,提示temp_tbl表不存存或无权限访问。
    请大神再帮忙优化下,谢谢啦。
    人家给的是实例,你需要把表替换成自己的表。错误已经很清楚了。