0

我创建了这个存储过程,但是我遇到了很多错误,以至于我无法弄清楚我的代码有什么问题。如果有人可以提供帮助,那就太棒了。

方向

使用事务处理编写一个存储过程,将新行插入Vendor表中,然后添加至少一个新的InvoiceInvoiceLineItems为那个新的Vendor。该程序必须包括处理失败事务的能力,在这种情况下,它应该发出错误消息和回滚。

我的代码:

USE AP

CREATE PROCEDURE Test02_sp 
AS
    @VendorName VARCHAR(50),
    @VendorCity VARCHAR(50),
    @VendorState CHAR(2),
    @VendorZipCode VARCHAR(20),
    @ShipName VARCHAR(MAX),
    @ShipRegion VARCHAR(MAX),
    @ShipPostalCode VARCHAR(MAX),
    @ShipCountry VARCHAR(MAX),
    @CustomerID INT,
    @CompanyName VARCHAR(MAX),
    @Address VARCHAR(MAX),
    @City VARCHAR(MAX),
    @Region VARCHAR(MAX),
    @PostalCode VARCHAR(MAX),
    @Country VARCHAR(MAX),
    @DefaultTermsID INT = NULL,
    @DefaultAccountNo INT = NULL,
BEGIN
    ALTER TABLE Vendor
        ADD DefaultTermsID INT NULL 
            CONSTRAINT Vendor_DefaultTermsID DEFAULT 3

    ALTER TABLE Vendor 
        ADD COLUMN VendorID INT PRIMARY KEY IDENTITY(1,1) NOT NULL
    GO

    ALTER TABLE Vendor 
        ADD DF_Vendors_AccountNo INT NULL 
            CONSTRAINT Vendor_DF_Vendors_AccountNo DEFAULT 570
    GO

    ALTER TABLE Vendor 
        ADD DefaultTermsID INT NULL 
            CONSTRAINT Vendor_DefaultTermsID DEFAULT 3
    GO

    INSERT INTO Vendor
    VALUES (@VendorName, @VendorCity, @VendorState, @VendorZipCode, @DefaultTermsID, @DefaultAccountNo)
    GO

    INSERT INTO Invoices 
    VALUES (@ShipName, @ShipAddress, @ShipCity, @ShipRegion, @ShipPostalCode, @ShipCountry, 
            @CustomerID, @CompanyName, @Address, @City, @Region, @PostalCode, @Country)
    GO
END
4

1 回答 1

0

您应该将事务应用于您的查询。

BEGIN TRANSACTION [Tran1]

SET XACT_ABORT OFF
BEGIN TRY

    ALTER TABLE Vendor
    ADD DefaultTermsID int NULL CONSTRAINT Vendor_DefaultTermsID DEFAULT 3

    ALTER TABLE Vendor ADD COLUMN VendorID int PRIMARY KEY IDENTITY(1,1) NOT NULL

    ALTER TABLE Vendor ADD DF_Vendors_AccountNo int NULL CONSTRAINT
    Vendor_DF_Vendors_AccountNo DEFAULT 570

    ALTER TABLE Vendor ADD DefaultTermsID int NULL CONSTRAINT Vendor_DefaultTermsID DEFAULT 3

    INSERT INTO Vendor
    VALUES (@VendorName,@VendorCity,@VendorState,@VendorZipCode,@DefaultTermsID,@DefaultAccountNo)

    INSERT INTO Invoices 
    VALUES (@ShipName,@ShipAddress,@ShipCity,@ShipRegion,@ShipPostalCode,@ShipCountry,@CustomerID,@CompanyName,@Address,@City,@Region,@PostalCode,@Country)


    COMMIT TRANSACTION [Tran1]

END TRY

BEGIN CATCH
    SELECT 'In Catch. Error occured', 4,  @@TRANCOUNT

     IF (XACT_STATE()) = 0
    BEGIN
        SELECT
            N'There is no transaction'

    END;


     IF (XACT_STATE()) = -1
    BEGIN
        SELECT
            N'The transaction is in an uncommittable state.' +
            'Rolling back transaction.'
        ROLLBACK TRANSACTION;
    END;

    -- Test whether the transaction is committable.
    IF (XACT_STATE()) = 1
    BEGIN
        SELECT
            N'The transaction is committable.' +
            'Committing transaction.'
        COMMIT TRANSACTION;   
    END;


END CATCH
于 2018-04-27T04:11:31.140 回答