sqlserver如何读写操作windows系统的文件
DECLARE @object int
DECLARE @hr int
DECLARE @src varchar(255), @desc varchar(255)
Declare @tmp int
declare @msg varchar(3000)
-------------------------1.写文件--------------------------------------
--将这串字符写到SQL Server的安装目录下的Nipsan.Txt文件里面
SET @msg='Hello. MS SQL Server 2000. I Love you!!!'
-- 取得SQL Server的安装路径
declare @strPath nvarchar(512)
Exec sp_MSGet_Setup_paths @strPath OUTPUT
--输出路径,文件名
Set @strPath=@strPath+'/Nipsan.Txt'
--创建Scripting组件实例
EXEC @hr = sp_OACreate 'Scripting.FileSystemObject', @object OUT
IF @hr <> 0
BEGIN
EXEC sp_OAGetErrorInfo @object, @src OUT, @desc OUT
SELECT hr=convert(varbinary(4),@hr), Source=@src, Description=@desc
RETURN
END
--创建文件
EXEC @hr = sp_OAMethod @object, 'CreateTextFile', @tmp OUTPUT , @strPath
IF @hr <> 0
BEGIN
EXEC sp_OAGetErrorInfo @object
RETURN
END
--把@msg写到文件里面去
EXEC @hr = sp_OAMethod @tmp, 'Write',NULL, @msg
IF @hr <> 0
BEGIN
EXEC sp_OAGetErrorInfo @object
RETURN
END
--关闭文件流
EXEC @hr = sp_OAMethod @tmp, 'Close',NULL
IF @hr <> 0
BEGIN
EXEC sp_OAGetErrorInfo @object
RETURN
END
------------------------------2.打开文件-------------------------------------------------
EXEC @hr = sp_OAMethod @object, 'OpenTextFile', @tmp OUTPUT ,@strPath
IF @hr <> 0
BEGIN
EXEC sp_OAGetErrorInfo @object
RETURN
END
------------------------------3.读取文件-------------------------------------------------
SET @msg=''
EXEC @hr = sp_OAMethod @tmp, 'Read', @msg OUT,3000
IF @hr <> 0
BEGIN
EXEC sp_OAGetErrorInfo @object
RETURN
END
SELECT @msg AS RESULT