无法使用存储过程从SQL Server表中删除记录

时间:2017-10-07 13:02:41

标签: sql sql-server database

我正在开发一个用于库存管理的桌面应用程序。

我在SQL Server中有3个表:

  1. TRANSACTIONS
  2. MATERIAL_TRANSACTION
  3. TRANSACTION_DETAILS
  4. 我已经创建了一个存储过程来插入和更新所有上面提到的表。

    TRANSACTIONS是我的主表。因此,我使用更新查询来更新其列。 但是在数据库中插入新行之前,需要删除其他两个表的记录,即详细表。

    我无法删除此存储过程中的行。请帮忙!

    我使用了以下SQL Server存储过程

    ALTER PROCEDURE [dbo].[SAVE_TRANSACTION] 
        @ID NVARCHAR(50),
        @REF_NO NVARCHAR(50),
        @VCH_DATE DATE,
        @VCH_TYPE INT,
        @CR_AC INT,
        @DR_AC INT,
        @DOCKET_NO NVARCHAR(50),
        @TRANSPORT NVARCHAR(50),
        @DELIVERY_NOTE NTEXT,
        @S_NAR NVARCHAR(100),
        @L_NAR NTEXT,
        @IS_REVERSE_CHARGE INT,
        @INVOICE_REF INT,
        @BUYER NVARCHAR(50),
        @BUYER_CONTACT NVARCHAR(50),
        @BUYER_ADDRESS NTEXT,
        @BUYER_GSTIN NVARCHAR(50),
        @BUYER_STATE NVARCHAR(50),
        @BUYER_REG_TYPE INT,
        @MATERIAL_TRANSACTION dbo.TVP_MATERIAL_TRANSACTION READONLY,
        @TRANSACTION_DETAILS dbo.TVP_TRANSACTION_DETAILS READONLY,
        @RETURN INT OUTPUT 
    AS
        DECLARE @TRAN_ID INT
    BEGIN
        SET NOCOUNT ON; 
    
        DELETE FROM MATERIAL_TRANSACTION 
        WHERE ID > 0 
          AND TRANSACTION_ID = @ID;
    
        DELETE FROM TRANSACTION_DETAILS 
        WHERE ID > 0 
          AND TRANSACTION_ID = @ID;
    
        IF (@ID = 0)
        BEGIN
            INSERT INTO dbo.TRANSACTIONS(REF_NO, VCH_DATE, VCH_TYPE, CR_AC, DR_AC,
                                         DOCKET_NO, TRANSPORT, DELIVERY_NOTE,
                                         S_NAR, L_NAR, IS_REVERSE_CHARGE,
                                         INVOICE_REF, BUYER, BUYER_CONTACT, BUYER_ADDRESS,
                                         BUYER_GSTIN, BUYER_STATE, BUYER_REG_TYPE)
            VALUES (@REF_NO, @VCH_DATE, @VCH_TYPE, @CR_AC, @DR_AC,
                    @DOCKET_NO, @TRANSPORT, @DELIVERY_NOTE,
                    @S_NAR, @L_NAR, @IS_REVERSE_CHARGE,
                    @INVOICE_REF, @BUYER, @BUYER_CONTACT, @BUYER_ADDRESS,
                    @BUYER_GSTIN, @BUYER_STATE, @BUYER_REG_TYPE);
    
            SET @TRAN_ID = @@IDENTITY
        END
        ELSE
        BEGIN
            UPDATE dbo.TRANSACTIONS
            SET REF_NO = @REF_NO,
                VCH_DATE = @VCH_DATE, VCH_TYPE = @VCH_TYPE,
                CR_AC = @CR_AC, DR_AC = @DR_AC,
                DOCKET_NO = @DOCKET_NO,
                TRANSPORT = @TRANSPORT, DELIVERY_NOTE = @DELIVERY_NOTE,
                S_NAR = @S_NAR, L_NAR = @L_NAR,
                IS_REVERSE_CHARGE = @IS_REVERSE_CHARGE,
                INVOICE_REF = @INVOICE_REF,
                BUYER = @BUYER, BUYER_CONTACT = @BUYER_CONTACT,
                BUYER_ADDRESS = @BUYER_ADDRESS,
                BUYER_GSTIN = @BUYER_GSTIN, BUYER_STATE = @BUYER_GSTIN,
                BUYER_REG_TYPE = @BUYER_REG_TYPE
            WHERE 
                ID = @ID;
    
            SET @TRAN_ID = @ID;
        END
    
        --UPDATE MATERIAL_TRANSACTION SET IS_DELETED=1 WHERE TRANSACTION_ID=@ID;
        --  UPDATE TRANSACTION_DETAILS SET IS_DELETED=1 WHERE TRANSACTION_ID=@ID;
        INSERT INTO TRANSACTION_DETAILS (TRANSACTION_ID, AC_ID, TRAN_TYPE,
                                         CR_AMT, DR_AMT, AMT_PERCENT, IS_PARENT)
            SELECT 
                @TRAN_ID, [AC_ID], [TRAN_TYPE], 
                [CR_AMT], [DR_AMT], [AMT_PERCENT], [IS_PARENT] 
            FROM 
                @TRANSACTION_DETAILS TVP
    
        INSERT INTO MATERIAL_TRANSACTION (TRANSACTION_ID, PRODUCT_ID, QTY,
                                          UNIT_ID, DESCRIPTION, HSN_CODE,
                                          PRICE, DISCOUNT_AMT, DISCOUNT_PERCENT,
                                          CGST_PERCENT, SGST_PERCENT, IGST_PERCENT,
                                          CGST_AMT, SGST_AMT, IGST_AMT)
            SELECT 
                @TRAN_ID, [PRODUCT_ID], [QTY],
                [UNIT_ID], [DESCRIPTION], [HSN_CODE],
                [PRICE], [DISCOUNT_AMT], [DISCOUNT_PERCENT],
                [CGST_PERCENT], [SGST_PERCENT], [IGST_PERCENT],
                [CGST_AMT], [SGST_AMT], [IGST_AMT] 
            FROM 
                @MATERIAL_TRANSACTION TVP
    
        SELECT @RETURN = @TRAN_ID
    END
    

0 个答案:

没有答案