qileilove

          blog已經(jīng)轉(zhuǎn)移至github,大家請訪問 http://qaseven.github.io/

          如何將SQL執(zhí)行的錯誤消息記錄到本地文件中

          其實大家都知道sql語句的錯誤信息都可以在sys.messages表里面找到

            如:

            如果在執(zhí)行語句在try...catch中,我們可以通過以下方法獲取錯誤信息。sql語句如下:

          BEGIN TRY
              SELECT  3 / 0
          END TRY
          BEGIN CATCH
              DECLARE @errornumber INT
              DECLARE @errorseverity INT
              DECLARE @errorstate INT
              DECLARE @errormessage NVARCHAR(4000)
              SELECT  @errornumber = ERROR_NUMBER() ,
                      @errorseverity = ERROR_SEVERITY() ,
                      @errorstate = ERROR_STATE() ,
                      @errormessage = ERROR_MESSAGE()

              SELECT  @errornumber ,
                      @errorseverity ,
                      @errorstate ,
                      @errormessage

              RAISERROR (
                       @errormessage, -- Message text,
                       @errorseverity,                        -- Severity,
                       @errorstate,                         -- State,
                     @errornumber
                    );
          END CATCH
          View Code

            當(dāng)然我這里是故意用RAISERROR再次拋出錯誤信息,運行結(jié)果如下:

            現(xiàn)在我們來定義一個存儲過程,其目的就是往本地文件中寫入信息。

            sql腳本如下:

          CREATE Proc [dbo].[UCreateOrAppendTextFile](@Filename VarChar(100),@Text nVarchar(4000))
          AS
          DECLARE @FileSystem int
          DECLARE @FileHandle int
          DECLARE @RetCode int
          DECLARE @RetVal int
          DECLARE @CreateOrAppend int


          EXECUTE @RetCode = sp_OACreate 'Scripting.FileSystemObject' , @FileSystem OUTPUT
          IF (@@ERROR|@RetCode > 0 Or @FileSystem < 0)
          RAISERROR ('could not create FileSystemObject',16,1)

          EXECUTE @RetCode = sp_OAMethod @FileSystem , 'FileExists', @RetVal out, @FileName
          IF (@@ERROR|@RetCode > 0)
          RAISERROR ('could not check file existence',16,1)
          -- If file exists then append else create
          SET @CreateOrAppend = case @RetVal when 1 then 8 else 2 end
          EXECUTE @RetCode = sp_OAMethod @FileSystem , 'OpenTextFile' , @FileHandle OUTPUT , @Filename, @CreateOrAppend, 1
          IF (@@ERROR|@RetCode > 0 Or @FileHandle < 0)
          RAISERROR ('could not create File',16,1)

          EXECUTE @RetCode = sp_OAMethod @FileHandle , 'WriteLine' , NULL , @text
          IF (@@ERROR|@RetCode > 0 )
          RAISERROR ('could not write to File',16,1)

          EXECUTE @RetCode = sp_OAMethod @FileHandle , 'Close'
          IF (@@ERROR|@RetCode > 0)
          RAISERROR ('Could not close file ',16,1)

          EXEC sp_OADestroy @filehandle
          IF (@@ERROR|@RetCode > 0)
          RAISERROR ('Could not destroy file object',16,1)

          EXEC sp_OADestroy @FileSystem
          ----------------------------------------

          然后執(zhí)行該存儲過程:

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

            如果遇到以下錯誤則說明Ole Automation Procedures沒有啟用

            需要執(zhí)行以下SQL:

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

            運行即如果如圖:

            當(dāng)然這里運行存儲過程之前必須保證 文件是存在的。

            最后封裝一個存儲過程獲取錯誤信息,其腳本如下:

          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

            執(zhí)行存儲過程及結(jié)果如下:

            以上存儲過程在MSSQL2005、2012中測試通過。

           大家都知道目前在文件系統(tǒng)中事務(wù)的實現(xiàn)還是比較復(fù)雜的,雖然在win7后我們可以用C#實現(xiàn)文件的事務(wù),但是微軟的分布式事務(wù)Distributed Transaction Coordinator(msdtc)目前也還不支持文件事務(wù)。

            這里說說為什么有這樣的需求吧:目前需要一個項目用SSIS做數(shù)據(jù)遷移,其中很大部分都是用sql語句實現(xiàn)的, 如 insert into ....select ... from xxxx.其中原數(shù)據(jù)庫中難免有什么臟數(shù)據(jù)導(dǎo)致插入失敗,于是我在SSIS中使用msdtc服務(wù),保證數(shù)據(jù)的一致性。雖然SSIS也有錯誤處理,但是它只 能記錄那個sql語句有問題,而不能記錄具體問題。于是我想到把錯誤信心記錄報數(shù)據(jù)庫表里面,可是當(dāng)遇到問題時事務(wù)會回滾,表里面根本就沒有錯誤信息。于 是乎 只能報錯誤信息記錄到文件中了。

            如:

            有不對的地方還請大家拍磚哦!

          posted on 2013-05-24 10:00 順其自然EVO 閱讀(216) 評論(0)  編輯  收藏 所屬分類: 數(shù)據(jù)庫

          <2013年5月>
          2829301234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 溧水县| 保亭| 隆化县| 克什克腾旗| 恭城| 台安县| 永宁县| 衡阳县| 泗洪县| 麻阳| 洛宁县| 德格县| 金川县| 台东县| 寿阳县| 新疆| 土默特左旗| 保康县| 高密市| 靖宇县| 大丰市| 北辰区| 泾川县| 巫溪县| 高密市| 鄂尔多斯市| 昌平区| 阳春市| 宜黄县| 乐都县| 台前县| 临猗县| 浠水县| 泽库县| 土默特右旗| 永平县| 汉沽区| 昆山市| 庆元县| 宜宾市| 张掖市|