淺析Oracle和SqlServer預存程序的調試、出錯處理

來源:互聯網
上載者:User

    在大型資料庫中,因為開發的需要,經常地需要調用Procedure,開發Procedure在Oracle和Sqlserver上因為文法不同,而有所區別,但是在調試上,都是比較的不容易,尤其是在一些錯誤處理上。

    首先說調試:

        1。對於Oracle的調試,可以藉助於第三方的工具,比如Pl/Sql Developer,我在用的版本是6.0.5.926;首先對該Procedure右鍵處理,添加“add debug information”,然後選擇“test”,開啟新的測試視窗,在下方對應的輸入輸出變數地方,添加相應的測試資料,注意:這裡的資料輸入,不需要引號,輸出參數不需要輸入

        然後點擊“start debugger”,或者按F9,進行測試,可以選擇測試的步驟如“step into”等,然後可以在下面的script視窗看到,中間變數;

        2。對於SqlServer調試,我還沒有找到比較好的第三方工具,目前是採用將中間的變數值或者sql語句插入到另一個表中,或者直接print出來的方式。第一種需要藉助 exec('') 方法,注意裡面的取變數值的寫法,可參考下面的例子,

                     exec('update SupplyplanLack set Completedate = GetDate()

                            from supplyPlanLack a where '+ @ssTmp +' and

                            '+@iCompleteQty+' >= (select s.RequestQty * a.RationQty as ReqQty

                                 from Balance a, SupplyPlan s where a.Vehicle = s.Vehicle

                                 and a.Part=s.Part and a.SupplyPlanNo = s.SupplyPlanNo

                                 and '+ @ssTmp +')')

        在print中,需要注意類型的轉換,一般是借用 convert(varchar(11),@spNOTo) 方法來實現,否則會提示類型轉換錯誤!

        3。錯誤處理上,對於Sqlserver可以採用開始自訂變數,然後根據不同判斷,改變該值然後推出的方法來處理,見下例 

set @exec_num=0

 if (@spNOFrom=0 or @spNOTo=0 or @reuseUser=NULL)
  begin
   set @exec_num=1
   goto the_end
  end

the_end:  
 return

        或者是這種:

 set nocount on

 if (@property is null) or (@property = '')
 begin
  raiserror('Must specify a property name.',-1,-1)
  return (1)
 end

        或者是對該錯誤全域變數 @@error 數值的判斷上

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.