How SQL Server reads and writes operating system files

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.