存储过程练习AP数据库SQL

时间:2018-04-27 01:56:41

标签: sql sql-server database error-messaging

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

路线

使用事务处理编写存储过程,该处理将新行插入var isDragging = false var fakeDraggingElement var currentDragging $('#item').mousedown(function(e){ e.preventDefault() isDragging = true currentDragging = this //change your cursor and make it loos like dragging $('#parent').css({cursor: 'move'}) //fake dragging element fakeDraggingElement = $(this).clone().css({ opacity:0.5, position:'absolute', top:e.pageY+offsetY, //parent offset left:e.pageX+offsetX,//parent offset }).appendTo(parent) }).mousemove(function(e){ if(!isDragging){ return } fakeDraggingElement.css({ top:e.pageY+offsetY, //parent offset left:e.pageX+offsetX,//parent offset }) }).mousedown(function(e){ if(!isDragging){ return } cancleDrag() //your old drop ondrop(currentDragging,e.target) }) function cancleDrag(){ isDragging = false $(currentDragging).remove() currentDragging = undefined }表,然后为新Vendor添加至少一个新InvoiceInvoiceLineItems。该程序必须包括处理失败事务的能力,在这种情况下它应该发出错误消息和回滚。

我的代码:

Vendor

1 个答案:

答案 0 :(得分:0)

您应该将Transaction应用于您的查询。

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