久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

重寫過程以避免插入然后回滾

Rewriting a proc to avoid an insert then rollback(重寫過程以避免插入然后回滾)
本文介紹了重寫過程以避免插入然后回滾的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

當我嘗試驗證以下內容時,我遇到了一個 proc 問題:

i am having an issue with a proc where when I try to validate the following:

  • @invoicenumber + @invoiceid 連接在一起使總發票號碼

  • @invoicenumber + the @invoiceid concatenate together to make the overall Invoicenumber

如果數據庫中已存在總發票編號,則拋出發票
號碼已存在'.

if overall invoicenumber already exists in database, throw 'Invoice
Number already exists'.

我遇到的問題是,即使發票編號不存在,它仍然會引發此錯誤.我想是因為它已經插入了數據,但插入后又回滾了,所以雖然看起來沒有插入表中,但它可能已經插入了,或者我是這么認為的.

The problem I am having is that even if the invoice number doesn't exist, it still throws this error. I think it is because it has inserted the data but is then rolledback after the insert, so though it looks like it's not inserted into the table, it may have already been inserted, or that's what I believe.

我的問題是有沒有辦法重寫這個過程以使其正常工作?也許先執行 SELECT 并進行驗證檢查,如果沒問題,然后開始在事務中執行插入?

My question is that is there a way to re-write this proc to get it working correctly? Maybe perform a SELECT first and do the validation checks and if that's ok then start performing the insert within the transaction?

被這個問題困住了一段時間,想看看是否有更有效的方法可以避免這個問題?

Been stuck on this issue for a while so like to see if there is a much more efficent way to see if this problem can be avoided?

下面是proc和exec:

Below is the proc and exec:

exec SupportAudit.BI.CreateMCCInvoiceReversal 'ABCD/000', 29923, 'ABC', 1

<小時>

USE [SupportAudit]
GO
/****** Object:  StoredProcedure [BI].[CreateMCCInvoiceReversal]    Script Date: 29/08/2016 07:23:09 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Procedure [BI].[CreateMCCInvoiceReversal]
(

    @InvoiceNumber      varchar(255),
    @InvoiceID          int,
    @DocType            varchar(15),
    @TaskLogid          int

)

AS

BEGIN

SET NOCOUNT ON;
SET XACT_ABORT ON;

declare @OutputList         [Core].[RollbackOutputList];
declare @procname           sysname; 


Set @procname = OBJECT_NAME(@@ProcID)


BEGIN TRY

BEGIN TRAN MCCInvoiceReversal

        INSERT INTO [Jet2Fees].Discount.Invoice
                (
                InvoiceNumber,
                DocType,
                )

        OUTPUT '[Jet2Fees].Discount.Invoice', 'InvoiceID', inserted.InvoiceId,
                                Core.insXMLFragment('InvoiceId')+Core.addnlXMLFragment('InvoiceId', inserted.InvoiceId)


            INTO @OutputList

            SELECT CONCAT(@InvoiceNumber, cast(InvoiceID as varchar(50))),
                    @DocType,

            FROM Jet2Fees.Discount.Invoice
            WHERE InvoiceId = @InvoiceId

    -- see if invoice number already exisits
if exists (select 1 from Jet2Fees.Discount.Invoice where InvoiceNumber = CONCAT(@InvoiceNumber, cast(@InvoiceID as varchar(50))))
            BEGIN;
                ROLLBACK TRAN MCCInvoiceReversal
                set @errormsg = 'Invoice Number already exists';
                THROW 99999, @errormsg, 1
            END;

exec Core.insertRollbackXML @outputList, @TaskLogid, @procname

COMMIT TRANSACTION MCCInvoiceReversal


END TRY

推薦答案

修改您的 TRY 塊,如下所示..

Modify your TRY block like below..

BEGIN TRY


IF exists (select 1 from Jet2Fees.Discount.Invoice where InvoiceNumber = CONCAT(@InvoiceNumber, cast(@InvoiceID as varchar(50))))
            BEGIN;
                set @errormsg = 'Invoice Number already exists';
                THROW 99999, @errormsg, 1
            END
ELSE 

     BEGIN
        INSERT INTO [Jet2Fees].Discount.Invoice
                (
                InvoiceNumber,
                DocType
                )

        OUTPUT '[Jet2Fees].Discount.Invoice', 'InvoiceID', inserted.InvoiceId,
                                Core.insXMLFragment('InvoiceId')+Core.addnlXMLFragment('InvoiceId', inserted.InvoiceId)


            INTO @OutputList

            SELECT CONCAT(@InvoiceNumber, cast(InvoiceID as varchar(50))),
                    @DocType
            FROM Jet2Fees.Discount.Invoice
            WHERE InvoiceId = @InvoiceId
          END

END TRY

這篇關于重寫過程以避免插入然后回滾的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Modify Existing decimal places info(修改現有小數位信息)
The correlation name #39;CONVERT#39; is specified multiple times(多次指定相關名稱“CONVERT)
T-SQL left join not returning null columns(T-SQL 左連接不返回空列)
remove duplicates from comma or pipeline operator string(從逗號或管道運算符字符串中刪除重復項)
Change an iterative query to a relational set-based query(將迭代查詢更改為基于關系集的查詢)
concatenate a zero onto sql server select value shows 4 digits still and not 5(將零連接到 sql server 選擇值仍然顯示 4 位而不是 5)
主站蜘蛛池模板: 成人在线免费视频观看 | 久久久久久久久久久久久久av | 亚洲综合婷婷 | 久久久久久久久久一区 | 亚洲一区二区免费看 | 日韩一区二区三区精品 | 久久精品久久久久久 | 2022精品国偷自产免费观看 | 亚洲va中文字幕 | av网站观看 | 亚洲高清在线观看 | 国产精品v | 亚洲视频免费观看 | 欧美在线一区二区三区 | 国产在线精品一区二区三区 | 精品久久一 | 国产成人精品久久二区二区91 | 亚洲精品视频网站在线观看 | 久久狠狠 | 丁香久久 | 亚洲一区二区三区四区五区中文 | 精品中文字幕在线观看 | 97高清国语自产拍 | 精品免费国产一区二区三区四区介绍 | 久久黄色精品视频 | 日本三级电影在线看 | 国产精品视频网站 | 日韩美女在线看免费观看 | 国产精品成人在线 | 亚洲免费婷婷 | 狠狠干美女| 欧美黑人激情 | 97久久精品午夜一区二区 | 日韩一区二区在线看 | 日韩在线视频免费观看 | 亚洲成人av在线播放 | www.亚洲一区二区 | 午夜视频导航 | 日本小电影在线 | 国产精品久久久久久久久久免费看 | 一级欧美 |