-- Name: Writing to Text File
-- Description:With this Stored procedur
-- e you can log messages to a text file
-- By: Anees Ahmed
--
-- Inputs:@text VARCHAR(100)=what is the
-- message you want to enter
--@file VARCHAR(100)=what IS the file name
--@overwrite BIT = 0
--
CREATE PROC writing_2_file
@text VARCHAR(100),
@file VARCHAR(100),
@overwrite BIT = 0
AS
BEGIN
SET NOCOUNT ON
DECLARE @query VARCHAR(255)
SET @query = RTRIM('echo ' + COALESCE(LTRIM(@text),'-') + CASE WHEN (@overwrite = 1) THEN ' > ' ELSE ' >> ' END + RTRIM(@file))
EXEC master..xp_cmdshell @query
PRINT @query
SET NOCOUNT OFF
END
GO
--exec writing_2_file 'There is already an object named writing_2_file in the database', 'c:\test.txt', 1
-- 0 appends and 1 overwrites