qileilove

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

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

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

            如:

            如果在執(zhí)行語句在try...catch中,我們可以通過以下方法獲取錯(cuò)誤信息。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再次拋出錯(cuò)誤信息,運(yùn)行結(jié)果如下:

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

            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í)行該存儲(chǔ)過程:

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

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

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

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

            運(yùn)行即如果如圖:

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

            最后封裝一個(gè)存儲(chǔ)過程獲取錯(cuò)誤信息,其腳本如下:

          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í)行存儲(chǔ)過程及結(jié)果如下:

            以上存儲(chǔ)過程在MSSQL2005、2012中測試通過。

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

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

            如:

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

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

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

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 九江县| 深州市| 辰溪县| 阳江市| 抚顺市| 潼关县| 衡东县| 卢湾区| 波密县| 土默特右旗| 嘉善县| 奈曼旗| 老河口市| 咸丰县| 潍坊市| 安图县| 伽师县| 贵南县| 黑水县| 郑州市| 东台市| 宣化县| 怀化市| 依安县| 双流县| 阿拉善盟| 离岛区| 龙岩市| 隆子县| 洪泽县| 岳阳市| 福贡县| 繁峙县| 宁国市| 平远县| 洛宁县| 廉江市| 温泉县| 那曲县| 望谟县| 郁南县|