sql server 讀寫txt檔案
阿新 • • 發佈:2021-02-02
測試環境:
sql server 的登入使用者為sa
步驟:
1 啟用xp_cmdshell元件
use master
go
sp_configure 'show advanced options',1
reconfigure
go
sp_configure 'xp_cmdshell',1
reconfigure
go
參考:https://www.cnblogs.com/atree/p/SQL_SERVER_xp_cmdshell.html
2 把文字寫入txt檔案
--把文字‘測試文字’ 寫入到 c:\test.txt 檔案中 exec master.dbo.xp_cmdshell 'echo 測試文字 > c:\test.txt'
參考:https://www.cnblogs.com/linjincheng/p/12198039.html
3 從文字檔案中讀取文字
--新建臨時表
CREATE TABLE testTable (data varchar(2000));
--把檔案中的資料匯入到臨時表中
BULK INSERT testTable
FROM 'c:\test.txt'
WITH
(
ROWTERMINATOR ='\n'
)
--查詢結果
select * from testTable;
查詢結果如下圖:
參考:http://www.voidcn.com/article/p-onodiaiy-bsz.html