首页 > 代码库 > Tran 与 Goto try catch raiserror等浅显应用
Tran 与 Goto try catch raiserror等浅显应用
---- Tran 事务 以及 Goto 的浅显应用IF OBJECT_ID(‘tempdb..#tmptab‘,‘U‘) IS NOT NULL DROP TABLE #tmptabGOCREATE TABLE #tmptab (id int IDENTITY(1,1),name VARCHAR(10),bankMoney MONEY)GOINSERT INTO #tmptab(name,bankMoney)SELECT ‘Jack‘,100UNIONSELECT ‘Frank‘,200GODECLARE @tran_errorcount INT=0BEGIN TRANSACTION bankTranBEGIN TRY --SELECT 1/0UPDATE #tmptab SET bankMoney=bankMoney+100 WHERE name=‘Jack‘UPDATE #tmptab SET bankMoney=bankMoney-100 WHERE name=‘Frank‘END TRYBEGIN CATCH SET @tran_errorcount+=1DECLARE @error_number INT =ERROR_NUMBER(), @error_msg VARCHAR(256)=ERROR_MESSAGE()RAISERROR(‘Error Member is: %i and error message: %s‘,-1,-1,@error_number,@error_msg) WITH NOWAITEND CATCHPRINT @tran_errorcountIF @tran_errorcount>0 GOTO error_processELSE GOTO success_processsuccess_process: RAISERROR(‘Transaction finished successfully! Congratulations!‘,-1,-1) WITH NOWAIT COMMIT TRAN GOTO end_process error_process: RAISERROR(‘Warning! Transaction has exception!‘,-1,-1) WITH NOWAIT ROLLBACK TRANSACTION GOTO end_processend_process: SELECT * FROM #tmptab RETURN
Tran 与 Goto try catch raiserror等浅显应用
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。