• 【转】SQL Server T-SQL写文本文件


    原文:http://www.nigelrivett.net/SQLTsql/WriteTextFile.html

    The are several methods of creating text files from sql server.

    • osql
    • bcp
    • redirection commands
    • sp_MakeWebTask
    • Com object calls

    Remember that in all cases the path of the output file will be relative to the server - not to the client on which the application is running.

    osql.
    This example will just output master..sysobjects to the file c:osqloutput.txt.

    declare @cmd varchar(1000)
    select @cmd = 'osql -U -P -S -Q"select * from master..sysobjects" -o"c:osqloutput.txt" -w500'
    exec master..xp_cmdshell @cmd

    bcp
    bcp very fast and easy to use for just dumping a table out - can be used against a view too.

    master..xp_cmdshell 'bcp master..sysobjects out c:file.bcp -S -U -P -c '

    redirection commands
    You will need to create the data to be output as in dynamic sql statements
    The first command here creates or overwrites the file - the rest append to the file.

    exec master..xp_cmdshell 'echo hello > c:file.txt'
    exec master..xp_cmdshell 'echo appended data >> c:file.txt'
    exec master..xp_cmdshell 'echo more data >> c:file.txt'

    will generate a file containing
    hello
    appended data
    more data

    sp_MakeWebTask
    This is for creating html code from a query

    Com object calls
    Using sp_oa... system stored procedures and creating com objects or existing applications you can probably do whatevr you wish - but you should probably ask whether sql server is the right place to control this.

  • 相关阅读:
    day30 python类的继承,抽象类等
    20170702-变量说明,静态方法,类方法区别,断点调试,fork,yield协程,进程,动态添加属性等。。
    day29 面向对象入门
    day28 import,from * import *,__name__
    day27 模块:正则re, configparser, subprocess
    day26 re正则表达式
    MD5的学习与练习
    HBase
    Struts13---Ognl
    Struts12---文件的下载
  • 原文地址:https://www.cnblogs.com/duanguyuan/p/4111094.html
Copyright © 2020-2023  润新知