然后执行该存储过程:

  exec UCreateOrAppendTextFile 'C:Error.log','hello majaing'

  如果遇到以下错误则说明Ole Automation Procedures没有启用

  需要执行以下SQL:

go
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Ole Automation Procedures', 1;
GO
RECONFIGURE;
GO

  运行即如果如图:

  当然这里运行存储过程之前必须保证 文件是存在的。

  后封装一个存储过程获取错误信息,其脚本如下:

CREATE PROCEDURE LOGError(@msg nvarchar(400))
as
declare @text nvarchar(400)
SELECT  @text=text FROM sys.messages WHERE language_id=1033 AND  message_id=@@ERROR
if len(@text)>1
begin
set @msg=@msg +' : '+@text
 EXEC dbo.UCreateOrAppendTextFile 'C:Error.log',@msg
end

  执行存储过程及结果如下:

 

  

以上存储过程在MSSQL2005、2012中测试通过。