0
我在下面的查詢中有一個嵌入式TRANSACTION的簡單TRY CATCH。
我有拼寫錯誤在查詢中的列名:ItemID1
第二UPDATE
內TRANSACTION
當我運行查詢,我得到它具有嚴重性高於10以下錯誤信息:SQL Server不'捕捉我的異常
消息207 ,級別16,狀態1,行xxx無效的列名稱'ItemID1'。
第一個UPDATE
成功運行(我希望它會ROLLBACK
)。
這表明它從未去過CATCH
塊。
根據我的理解,CATCH
捕獲高於嚴重性10的錯誤。這是嚴重性16,因此它應該已經捕獲它。 任何想法爲什麼CATCH
從未執行?
BEGIN TRY
IF OBJECT_ID('tempdb..#MyTempTable') IS NOT NULL
DROP TABLE #MyTempTable
CREATE TABLE #MyTempTable
(
ObjectID UNIQUEIDENTIFIER , OrgID NVARCHAR(20)
)
INSERT INTO #MyTempTable
SELECT ObjectTable1.ObjectID AS ObjectID ,OrgID
FROM ObjectTable1 WITH (NOLOCK) INNER JOIN Org WITH (NOLOCK) ON ObjectTable1.OrgID = Org.OrgID
WHERE OrgID IN ('Blah1', 'Blah2')
INSERT INTO #MyTempTable
SELECT ObjectTable2.ObjectID AS ObjectID, OrgID
FROM ObjectTable2 WITH (NOLOCK) INNER JOIN Org WITH (NOLOCK) ON ObjectTable2.OrgID = Org.OrgID
WHERE OrgID IN ('Blah3', 'Blah4')
SELECT * FROM #MyTempTable
BEGIN TRANSACTION
UPDATE ObjectTable1
SET ItemID = NULL FROM ObjectTable1 INNER JOIN #MyTempTable ON #MyTempTable.ObjectID = ObjectTable1.ObjectID
PRINT 'Updated ' + CAST(@@ROWCOUNT AS VARCHAR(20)) + ' Objects in ObjectTable1'
UPDATE ObjectTable2
SET ItemID = (ItemID1 + 1) FROM ObjectTable2 INNER JOIN #MyTempTable ON #MyTempTable.ObjectID = ObjectTable2.ObjectID
PRINT 'Updated ' + CAST(@@ROWCOUNT AS VARCHAR(20)) + ' Objects in ObjectTable2'
COMMIT
DROP TABLE #MyTempTable
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0
ROLLBACK
DROP TABLE #MyTempTable
DECLARE @ErrMsg NVARCHAR(4000) ,
@ErrSeverity INT
SELECT @ErrMsg = ERROR_MESSAGE() ,
@ErrSeverity = ERROR_SEVERITY()
RAISERROR(@ErrMsg, @ErrSeverity, 1)
END CATCH