Thursday, September 27, 2007

[mssql] Write to Text File

CREATE PROCEDURE usp_WriteToTxtFile(
@pFileName varchar(255),
@pText varchar(255))
AS
DECLARE @iFS int,
@iOLEResult int,
@iFileID int,

EXECUTE @iOLEResult = sp_OACreate 'Scripting.FileSystemObject', @iFS OUT
IF @iOLEResult <> 0 PRINT 'Scripting.FileSystemObject'

--Open a file
EXECUTE @iOLEResult = sp_OAMethod @FS, 'OpenTextFile', @iFileID OUT, @pFileName, 8, 1
IF @iOLEResult <> 0 PRINT 'OpenTextFile'

--Write the input Text
EXECUTE @iOLEResult = sp_OAMethod @iFileID, 'WriteLine', Null, @pText
IF @iOLEResult <> 0 PRINT 'WriteLine'

EXECUTE @iOLEResult = sp_OADestroy @iFileID
EXECUTE @iOLEResult = sp_OADestroy @iFS
GO

http://www.motobit.com/tips/detpg_SQLWrFile/

No comments:

Post a Comment