如何使用MSSQL 2000讀取/寫入文件?

[英]How can I read/write from/to files with MSSQL 2000?


In MySQL you can quite simply read a file with SELECT load_file('foo.txt') and write to a file with SELECT 'text' INTO DUMPFILE 'foo.txt'.

在MySQL中,您可以使用SELECT load_file('foo.txt')簡單地讀取文件,並使用SELECT'text'INTO DUMPFILE'foo.txt'寫入文件。

I am looking for a way to do this in MSSQL 2000 without using any external tools, just with queries or stored procedures.

我正在尋找一種在MSSQL 2000中執行此操作的方法,而無需使用任何外部工具,只需查詢或存儲過程。

3 个解决方案

#1


For input, you can use the text file ODBC driver:

對於輸入,您可以使用文本文件ODBC驅動程序:

select * from OpenRowset('MSDASQL',
'Driver={Microsoft Text Driver (*.txt;
*.csv)};DefaultDir=c:\;','select * from [FileName.csv]')

Assuming the output file already exists, you can use Jet to write to it:

假設輸出文件已經存在,您可以使用Jet寫入它:

INSERT INTO
OPENROWSET('Microsoft.Jet.OLEDB.4.0', 
'Text;Database=C:\;HDR=Yes;', 'SELECT
* FROM filename.csv')   SELECT 1234,5678

#2


There are a few ways you can do this.

有幾種方法可以做到這一點。

using xp_cmdshell:

declare @writetofile varchar(1000)
select @writetofile = 'osql -U -P -S -Q"select * from yourtable" -o"c:\foo.txt"'

exec master..xp_cmdshell @writetofile 

Or you can use bcp from the command line to read/write to files, which is usually quicker to do bulk inserts/selects

或者,您可以從命令行使用bcp來讀取/寫入文件,這通常可以更快地進行批量插入/選擇

Alternatively, you can also use sp_OACreate, but you probably don't want to go this route since it's not exactly what databases are meant to do. :)

或者,您也可以使用sp_OACreate,但您可能不想使用此路由,因為它不完全是數據庫的意圖。 :)

#3


You can also use BULK INSERT to read files.

您還可以使用BULK INSERT來讀取文件。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/02/17/547b68b4bf2dff14d4dfd61cf3077da8.html



 
  © 2014-2022 ITdaan.com 联系我们: