Laboratory/MSSQL

Sp_OAMethod로 Scripting.FileSystemObject를 사용하여 MSSQL에서 파일컨트롤

theking 2010. 6. 30. 15:45

DECLARE @FS INT

DECLARE @RC INT

DECLARE @FileID INT

DECLARE @Query VARCHAR(1000)

DECLARE @FileName VARCHAR(1000)

SET @FileName = 'C:\Authors.txt'

-- A. OLE개체인스턴스생성

EXEC @RC = sp_OACreate 'Scripting.FileSystemObject', @FS OUT

IF @RC <> 0

BEGIN

PRINT 'Error: Creating the file system object'

END

-- B. File Open

EXEC @RC = sp_OAMethod @FS, 'OpenTextFile' , @FileID OUT, @FileName, 8, 1

IF @RC <> 0

BEGIN

PRINT 'Error: Opening the specified text file'

END

-- C. Write

EXEC @RC = sp_OAMethod @FileID, 'WriteLine', Null, '테스트'

IF @RC <> 0

BEGIN

PRINT 'Error: Writing string data to file'

END

-- D.OLE 개체를삭제

EXECUTE @RC = sp_OADestroy @FileID

EXECUTE @RC = sp_OADestroy @FS

 

 

출처 : http://blog.naver.com/mist80?Redirect=Log&logNo=40011132403