"Go" SQL Server T-SQL Write text file

Source: Internet
Author: User

Original: http://www.nigelrivett.net/SQLTsql/WriteTextFile.html

The is 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 is relative to the server-not to the client on which the app Lication is running.

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

Declare @cmd varchar (+) Select @cmd = ' osql-u-p-s-Q "select * from Master. sysobjects "-O" c:\osqloutput.txt "-w500'exec@cmd

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

"'

Redirection commands
You'll need to create the data to is output as in dynamic SQL statements
The first command here creates or overwrites the File-the rest append to the file.

exec ' echo Hello > C:\file.txt ' exec ' Echo appended data >> C:\file.txt ' exec ' echo More data >> c:\file.txt '

Would generate a file containing
Hello
Appended data
More data

Sp_makewebtask
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 does WHATEVR you Wish-but should probably ask whether SQL Server is the right place to control this.

"Go" SQL Server T-SQL Write text file

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.