1.目的:存储过程执行后,从数据库某一表中取出数据存放到自定义的TXT文件中
    
    期待有相关成功经验的大神赐教,不胜感谢!

解决方案 »

  1.   

    取出数据存放到自定义的TXT文件中?为嘛是txt文件呢?好像是可以直接导出到excel或者xml中的
      

  2.   

    客户要求是直接在存储过程中执行相关程序,直接导出txt文件,却是很难弄,搞了2天,只好来这里求助了!
      

  3.   

    用xp_cmdshell执行bcp命令导出.
      

  4.   

    不适用BCP的话,还有其他方法么?最好贴上大神们的代码!万分感谢
      

  5.   

    据我所知只能用bcp
    bcp 也可以写到存储过程里面啊
      

  6.   

    关于BCP 写入存储过程该如何实现呢?希望有代码内容,这样能更好理解
      

  7.   


    调用  master..xp_cmdshell 存储过程 
    不过需要启动xp_cmdshell启动语句如下-- 允许配置高级选项 
    EXEC sp_configure 'show advanced options', 1 
    GO 
    -- 重新配置 
    RECONFIGURE 
    GO 
    -- 启用xp_cmdshell 
    EXEC sp_configure 'xp_cmdshell', 1 
    GO 
    --重新配置 
    RECONFIGURE 
    GO
    -- 关闭允许配置高级选项 
    EXEC sp_configure 'show advanced options', 0
    GO 下面是调用bcp的例子
    EXEC master..xp_cmdshell 'bcp 库名.dbo.表名out c:\Temp.xls -c -q -S"servername" -U"sa" -P""'
    参数:S 是SQL服务器名;U是用户;P是密码EXEC master..xp_cmdshell 'bcp axzq.dbo.Staff out d:staff.xls -c -q -S"." -U"sa" -P"gazx"'说明:还可以导出文本文件等多种格式
    实例:EXEC master..xp_cmdshell 'bcp saletesttmp.dbo.CusAccount out c:\temp1.xls -c -q -S"pmserver" -U"sa" -P"sa"'
    EXEC master..xp_cmdshell 'bcp "SELECT au_fname, au_lname FROM pubs..authors ORDER BY au_lname" queryout C:\ authors.xls -c -Sservername -Usa -Ppassword'EXEC master..xp_cmdshell 'bcp "select name from axzq..staff order by name"  queryout d:staffName.xls -c -q -S"." -U"sa" -P"gazx"'