Tuesday, March 6, 2012

acess I/O File

Is it possible to access a file (for example, to write in a text file)
with transact-sql?.
I've create a stored procedure and I'd like to write in a log file
some traces depends on the result of the statements executed in this
stored procedure.
ThanksHere's one method:

ALTER PROC WriteToLog
@.Message varchar(100)
AS
DECLARE @.Command varchar(255)
BEGIN TRAN
-- ensure only one process writes to the log at a time
EXEC sp_getapplock
@.Resource = 'MyLog.txt',
@.lockmode = 'Exclusive'
SET @.Command = 'ECHO ' +
@.Message +
' >>"C:\MyLog.txt"'
EXEC master..xp_cmdshell @.Command, NO_OUTPUT
EXEC sp_releaseapplock @.Resource = 'MyLog.txt'
COMMIT
GO

EXEC WriteToLog 'Test message'

--
Hope this helps.

Dan Guzman
SQL Server MVP

--------
SQL FAQ links (courtesy Neil Pike):

http://www.ntfaq.com/Articles/Index...epartmentID=800
http://www.sqlserverfaq.com
http://www.mssqlserver.com/faq
--------

"Bego?a" <bego_colomer@.yahoo.es> wrote in message
news:c04c2a85.0311180409.4631ec98@.posting.google.c om...
> Is it possible to access a file (for example, to write in a text file)
> with transact-sql?.
> I've create a stored procedure and I'd like to write in a log file
> some traces depends on the result of the statements executed in this
> stored procedure.
> Thanks

No comments:

Post a Comment