取消交易 |
尚未結案
|
kissdelphi
一般會員 ![]() ![]() 發表:7 回覆:9 積分:3 註冊:2003-01-09 發送簡訊給我 |
|
kissdelphi
一般會員 ![]() ![]() 發表:7 回覆:9 積分:3 註冊:2003-01-09 發送簡訊給我 |
|
Mickey
版主 ![]() ![]() ![]() ![]() ![]() 發表:77 回覆:1882 積分:1390 註冊:2002-12-11 發送簡訊給我 |
|
kissdelphi
一般會員 ![]() ![]() 發表:7 回覆:9 積分:3 註冊:2003-01-09 發送簡訊給我 |
kissdelphi 你好: 一個 Transaction...異動十萬筆記錄...對 DB Server 來說, 是很沉重得負擔喔. 你省略的部分...可能是原因所在...建議你 po 上, "if @@error=0..." 之前幾句 T-SQL,
因為 @@error 是 MSSQL 的 Global 變數, 若你的前一句 T-SQL 未發生 Error,
則 @@error 便是 0. -------------------------------------------------------------------
Mickey:
省略部分由許多分隔的sql語句組成,影響不同的表。依你的意思,我要再沒個影響表的動作結束後都加以判斷嗎?
@@Error表示最近的一段sql語句沒發生錯誤?
Thanks.
|
Mickey
版主 ![]() ![]() ![]() ![]() ![]() 發表:77 回覆:1882 積分:1390 註冊:2002-12-11 發送簡訊給我 |
引言: 省略部分由許多分隔的sql語句組成,影響不同的表。依你的意思,我要再沒個影響表的動作結束後都加以判斷嗎? @@Error表示最近的一段sql語句沒發生錯誤?依據我的了解, 是這樣的. 不過...我習慣這樣寫 : begin begin transaction ... if @@error<>0 then goto error ... if @@error<>0 then goto error ... commit transaction error: raiserror 50000 '資料交易失敗.' rollback transaction end |
timhuang
尊榮會員 ![]() ![]() ![]() ![]() ![]() ![]() 發表:78 回覆:1815 積分:1608 註冊:2002-07-15 發送簡訊給我 |
補充一下, mickey 兄所說的沒錯, @@error 變數確實是以最後一個 statement 的成功或失敗為準來出現 0或非 0, 以下為引述 online help 中的內容,
當 Microsoft® SQL Server™ 完成執行 Transact-SQL 陳述式時,如果此陳述式順利執行,則 @@ERROR 將設定為 0。若是發生錯誤,則會傳回錯誤訊息。@@ERROR 會傳回錯誤訊息的代碼,直到執行另一個 Transact-SQL 陳述式。可以在 sysmessages 系統資料表中檢視有關 @@ERROR 錯誤代碼的文字。 因為在執行各陳述式時會清除和重設 @@ERROR,請在驗證陳述式之後立即檢查它,或將它存入稍後可以檢查的本機變數中。詳細的內容及範例的使用方式可以參考 help 中的 @@error 段, 另外也引述一個其中的 sample code 給你參考: C. 使用 @@ERROR 來檢查數個陳述式是否成功 本範例以 INSERT 和 DELETE 陳述式的成功作業為根據。本機變數將在兩個陳述式之後設定為 @@ERROR 的值,並用於該作業的共用錯誤處理常式中。 USE pubs GO DECLARE @del_error int, @ins_error int -- Start a transaction. BEGIN TRAN -- Execute the DELETE statement. DELETE authors WHERE au_id = '409-56-7088' -- Set a variable to the error value for -- the DELETE statement. SELECT @del_error = @@ERROR -- Execute the INSERT statement. INSERT authors VALUES('409-56-7008', 'Bennet', 'Abraham', '415 658-9932', '6223 Bateman St.', 'Berkeley', 'CA', '94705', 1) -- Set a variable to the error value for -- the INSERT statement. SELECT @ins_error = @@ERROR -- Test the error values. IF @del_error = 0 AND @ins_error = 0 BEGIN -- Success. Commit the transaction. PRINT "The author information has been replaced" COMMIT TRAN END ELSE BEGIN -- An error occurred. Indicate which operation(s) failed -- and roll back the transaction. IF @del_error <> 0 PRINT "An error occurred during execution of the DELETE statement." IF @ins_error <> 0 PRINT "An error occurred during execution of the INSERT statement." ROLLBACK TRAN END GO |
kissdelphi
一般會員 ![]() ![]() 發表:7 回覆:9 積分:3 註冊:2003-01-09 發送簡訊給我 |
引言: 補充一下, mickey 兄所說的沒錯, @@error 變數確實是以最後一個 statement 的成功或失敗為準來出現 0或非 0, 以下為引述 online help 中的內容,多謝指教,其實幫助中說的很明白,我應該先看幫助再來提問題的。不好意思,佔用大家的寶貴時間,浪費了有限的網絡資源。下次一定注意! 多謝各位參與 !當 Microsoft® SQL Server™ 完成執行 Transact-SQL 陳述式時,如果此陳述式順利執行,則 @@ERROR 將設定為 0。若是發生錯誤,則會傳回錯誤訊息。@@ERROR 會傳回錯誤訊息的代碼,直到執行另一個 Transact-SQL 陳述式。可以在 sysmessages 系統資料表中檢視有關 @@ERROR 錯誤代碼的文字。 因為在執行各陳述式時會清除和重設 @@ERROR,請在驗證陳述式之後立即檢查它,或將它存入稍後可以檢查的本機變數中。詳細的內容及範例的使用方式可以參考 help 中的 @@error 段, 另外也引述一個其中的 sample code 給你參考:C. 使用 @@ERROR 來檢查數個陳述式是否成功 本範例以 INSERT 和 DELETE 陳述式的成功作業為根據。本機變數將在兩個陳述式之後設定為 @@ERROR 的值,並用於該作業的共用錯誤處理常式中。 USE pubs GO DECLARE @del_error int, @ins_error int -- Start a transaction. BEGIN TRAN -- Execute the DELETE statement. DELETE authors WHERE au_id = '409-56-7088' -- Set a variable to the error value for -- the DELETE statement. SELECT @del_error = @@ERROR -- Execute the INSERT statement. INSERT authors VALUES('409-56-7008', 'Bennet', 'Abraham', '415 658-9932', '6223 Bateman St.', 'Berkeley', 'CA', '94705', 1) -- Set a variable to the error value for -- the INSERT statement. SELECT @ins_error = @@ERROR -- Test the error values. IF @del_error = 0 AND @ins_error = 0 BEGIN -- Success. Commit the transaction. PRINT "The author information has been replaced" COMMIT TRAN END ELSE BEGIN -- An error occurred. Indicate which operation(s) failed -- and roll back the transaction. IF @del_error <> 0 PRINT "An error occurred during execution of the DELETE statement." IF @ins_error <> 0 PRINT "An error occurred during execution of the INSERT statement." ROLLBACK TRAN END GO |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |