程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> SqlServer數據庫 >> 關於SqlServer >> 使用存儲過程寫txt文本的方法匯集

使用存儲過程寫txt文本的方法匯集

編輯:關於SqlServer

-- Writing to a text file from a stored procedure
Author Nigel Rivett

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.


home  
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved