在大型資料庫中,因為開發的需要,經常地需要調用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 數值的判斷上