當前位置:首頁 » 存儲配置 » sql存儲過程錯誤處理

sql存儲過程錯誤處理

發布時間: 2025-04-10 21:06:18

A. sql存儲過程中事務出現錯誤回滾,那麼在回滾之後的語句會執行嗎

會的。

一般回滾操作都是寫在異常處理,或是sql的最後。如果你的sql中出現錯誤 ,代碼會立即跳轉到錯誤處理代碼上執行,比如回滾,但緊接在錯誤行之後的代碼不會執行的。


1.update .....;

2.select ......;
3.when Exception
....rollback;
4.insert into .....

以上偽代碼,如果行1出錯,行2將不會執行,直接跳轉到行3,然後行4 也會執行。

B. 在sql2005資料庫執行存儲過程出現:關鍵字 'SET' 附近有語法錯誤。 怎麼處理呀代碼如下

換成

CREATE proc [dbo].[GetDataSet]
@TableList Varchar(1024)='*',--搜索表的欄位,比如:』id,datatime,job『,用逗號隔開
@TableName Varchar(100), --搜索的表名
@SelectWhere Varchar(1024)='',--搜索條件,這里不用寫where,比如:job=』teacher『and class='2'
@SelectOrderId Varchar(50),--表主鍵欄位名。比如:id
@SelectOrder Varchar(200)='', --排序,可以使用多欄位排序但主鍵欄位必需在最前面.也可以不寫,比如:order by class asc
@intPageNo int=1, --頁號
@intPageSize int=10 ,--每頁顯示數
@RecordCount int OUTPUT --總記錄數(存儲過程輸出參數)
as
declare @TmpSelect NVarchar(4000)
declare @Tmp NVarchar(4000)
declare @pagecount int
set nocount on--關閉計數
set @RecordCount=0
set @TmpSelect = 'select @RecordCount=count(*) from '+@TableName+' where '+@SelectWhere
execute sp_executesql
@TmpSelect, --執行上面的sql語句
N'@RecordCount int OUTPUT' , --執行輸出數據的sql語句,output出總記錄數
@RecordCount OUTPUT
if (@RecordCount = 0) --如果沒有貼子,則返回零
return 0
set @pagecount = @RecordCount/@intPageSize
if(@RecordCount%@intPageSize>0)
set @pagecount=@pagecount+1
if(@pagecount<=0)
set @pagecount=1
if(@intPageNo>@pagecount)
set @intPageNo=@pagecount
/*判斷頁數是否正確*/
--if (@intPageNo - 1) * @intPageSize > @RecordCount --頁號大於總頁數,返回錯誤
--set @intPageNo=@pagecount
set nocount off--打開計數
if @SelectWhere != ''
begin
set @TmpSelect = 'select top '+str(@intPageSize)+' '+@TableList+' from '+@TableName+' where '+@SelectOrderId+' not in(select top '+str((@intPageNo-1)*@intPageSize)+' '+@SelectOrderId+' from '+@TableName+' where '+@SelectWhere +' '+@SelectOrder+') and '+@SelectWhere +' '+@SelectOrder
end
else
begin
set @TmpSelect = 'select top '+str(@intPageSize)+' '+@TableList+' from '+@TableName+' where '+@SelectOrderId+' not in(select top '+str((@intPageNo-1)*@intPageSize)+' '+@SelectOrderId+' from '+@TableName+''+@SelectOrder+') '+@SelectOrder
end
execute sp_executesql @TmpSelect
return(@@rowcount)
GO

熱點內容
java返回this 發布:2025-10-20 08:28:16 瀏覽:585
製作腳本網站 發布:2025-10-20 08:17:34 瀏覽:881
python中的init方法 發布:2025-10-20 08:17:33 瀏覽:574
圖案密碼什麼意思 發布:2025-10-20 08:16:56 瀏覽:761
怎麼清理微信視頻緩存 發布:2025-10-20 08:12:37 瀏覽:677
c語言編譯器怎麼看執行過程 發布:2025-10-20 08:00:32 瀏覽:1005
郵箱如何填寫發信伺服器 發布:2025-10-20 07:45:27 瀏覽:250
shell腳本入門案例 發布:2025-10-20 07:44:45 瀏覽:108
怎麼上傳照片瀏覽上傳 發布:2025-10-20 07:44:03 瀏覽:799
python股票數據獲取 發布:2025-10-20 07:39:44 瀏覽:705