码迷,mamicode.com
首页 > 数据库 > 详细

SQL SERVER调用textcopy写文件

时间:2016-10-29 18:49:41      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:status   cursor   服务   txt   master   ***   extc   查询   code   

SET @PATH = ‘textcopy /S ‘ + @LServer +

        ‘ /U ‘+ @LUser +

        ‘ /P ‘+ @LPass +

        ‘ /D ‘+ @LDB +

        ‘ /T ‘+@table+

        ‘ /C ‘ +@column+

        ‘ /W ‘+@Lwhere

        ‘ /F ‘ + @LPath +‘\‘+  @File + ‘ /O‘

EXEC master..xp_cmdshell @Path

/**说明

@LServer:服务器名

@LUser:用户名 

@LPass:密码

@LDB:数据库

@table:表名

@column:列名

@Lwhere:查询条件

@LPath:路径

@File: 文件名

***/

 

 一个成功的实例 :

declare @where varchar(200)
declare @file varchar(500)
declare @PATH varchar(800)
declare @id int,@stockcode  varchar(20),@stocksname varchar(20),@declaredate datetime,@F001 varchar(20)

declare sp cursor for
select id,stockcode,stocksname,declaredate,f001
from  t1
open sp
fetch next from sp into @id,@stockcode,@stocksname,@declaredate,@F001
while(@@fetch_status=0)
begin
set @where=‘"where id=‘+convert(varchar(20),@id)+‘"‘
set @file=‘(‘+@stockcode+‘)‘+@stocksname+‘(‘+convert(char(8),@declaredate,112)+‘)‘+@F001+‘.txt‘
SET @PATH = ‘textcopy /S yubaolong\s2‘ + 
        ‘ /U ‘+ ‘sa‘ +
        ‘ /P ‘+ ‘123‘ +
        ‘ /D ‘+ ‘db40‘+
        ‘ /T ‘+‘t1‘+
        ‘ /C ‘ +‘F002‘+
        ‘ /W ‘+@where+‘ /F ‘ + ‘d:\111‘ +‘\‘+@file+ ‘ /O‘
--        ‘" /F ‘ + ‘F:\PDF\11.PDF‘ + ‘ /O‘ 
EXEC master..xp_cmdshell @Path

 fetch next from sp into @id,@stockcode,@stocksname,@declaredate,@F001
end
close sp
deallocate sp

 

-------------------------------------------------------------------

SQL SERVER调用textcopy写文件

标签:status   cursor   服务   txt   master   ***   extc   查询   code   

原文地址:http://www.cnblogs.com/champaign/p/6011261.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!