0
我創建了一個存儲過程,在發生錯誤時它應該回滾所有內容,我一直在尋找,但是我找不到彈出的錯誤。關於事務不匹配的SQL存儲過程
[例外:在源「.net SqlClient數據提供」與過程‘ST_IV_ItemPrice.SP_Insert’錯誤數266.16.2發生在線路130事務計數EXECUTE後意外的SQL錯誤表示BEGIN的不匹配數和COMMIT語句。以前的計數= 1,當前計數= 0.]
我希望我可以在這裏得到一些幫助。
CREATE PROCEDURE [dbo].[ST_IV_ItemPrice.SP_Insert]
/*parameters*/
AS
BEGIN
IF OBJECT_ID(''tempdb..#tempPriceList'') IS NOT NULL
/*Then it exists*/
DROP TABLE #tempPriceList
CREATE TABLE #tempPriceList(PriceListid int, NewCurrencyUnitPrice decimal(19, 5))
DECLARE @Error int,
@NextListid int,
@NewCurrencyUnitPrice decimal(19, 5),
@NoRounding int = 0,
@UserSpecified int = 4
BEGIN TRANSACTION
BEGIN TRY
/*Insert item to table*/
SET @Id = SCOPE_IDENTITY()
SET @Error = @@ERROR
IF @Error = 0 AND @AutoGenerate = 1
BEGIN
INSERT INTO #tempPriceList(PriceListid, NewCurrencyUnitPrice)
VALUES(@Id, @Price)
WHILE(EXISTS(SELECT * FROM #tempPriceList))
BEGIN
SELECT TOP 1 @NextListid = [id], @NewCurrencyUnitPrice = [NewCurrencyUnitPrice]
FROM #tempPriceList
/*INSERT SELECT STATEMENT*/
INSERT INTO #tempPriceList ([PriceListid],[NewCurrencyUnitPrice])
Select [ListId] , [NewCurrencyUnitPrice]
IF @Error = 0 AND @SetExpiredDate = 1 AND @FromDate IS NOT NULL
BEGIN
/*Update item that is same as inserted and set to inactive*/
END
DELETE FROM #tempPriceList WHERE PriceListid = @NextListid
END
END
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION
END CATCH
IF @@TRANCOUNT>0
COMMIT TRANSACTION
RETURN @Error
END
怎麼寫,但我仍然得到同樣的錯誤 「事務計數EXECUTE後,我試圖表明BEGIN和COMMIT語句的不匹配數。一個計數= 1,當前計數= 0] 「 –
我計劃不要增加錯誤,因爲我想在我的程序中處理它,並且不希望網站進入用戶無法理解的錯誤頁面 –
當您陷入」CATCH「或當一切都好? –