【轉】SQL Server T-SQL寫文字檔

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   os   sp   for   

原文: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.

【轉】SQL Server T-SQL寫文字檔

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.