测试环境:
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