DECLARE @object int
DECLARE @hr int
DECLARE @src varchar (255), @desc varchar (255)
Declare @tmp int
DECLARE @msg varchar (3000)
-------------------------1. Write the file--------------------------------------
--write this string of characters into the Nipsan.Txt file in the installation directory of SQL Server
SET @msg = ' Hello '. MS SQL Server 2000. I Love you!!! '
--Get the installation path for SQL Server
DECLARE @strPath nvarchar (512)
Exec sp_msget_setup_paths @strPath OUTPUT
--Output path, file name
Set @[email protected]+ '/nipsan.txt '
--Create scripting Component Instances
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), [email protected], [email protected]
RETURN
END
--Create a file
EXEC @hr = sp_OAMethod @object, ' CreateTextFile ', @tmp OUTPUT, @strPath
IF @hr <> 0
BEGIN
EXEC sp_OAGetErrorInfo @object
RETURN
END
--Write the @msg to the file.
EXEC @hr = sp_OAMethod @tmp, ' Write ', NULL, @msg
IF @hr <> 0
BEGIN
EXEC sp_OAGetErrorInfo @object
RETURN
END
--close the file stream
exec @hr = sp_oamethod @tmp, ' Close ', null
IF @hr <> 0
begin
exec sp_oageterrorinfo @object
return
end
------------------------------2. Open file-------------------------------------------------
EXEC @hr = sp_oamethod @object, ' OpenTextFile ', @tmp output , @strPath
if @hr <> 0
begin
exec sp_oageterrorinfo @object
return
end
------------------------------3. Read the file-------------------------------------------------
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
How SQL Server reads and writes operating system files