Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Table Trigger Problems

$
0
0

I am currently working on an integration of FedEx Ship Manager Software and our accounting system.  The integration has a view which pulls Order and Shipping information from the database.  When a user enters a sales order, the data from a SQL database is pulled to the correct fields in FedEx Ship Manager (FSM).  When the package is shipped, FSM then sends a record to a table in the SQL database.  I want to write a trigger that takes 4 pieces of information from FSM (Tracking Number, Net Charge, Sales Order Number, and a flag to indicate if the shipment was deleted), and do the following: 

If the sales rep entered a freight charge on the sales order, update the cost and GM% on the order. 

If the sales rep did not enter a freight charge, insert a freight charge of 0.00 sales and update the cost. 

Then insert a text line which shows "FedEx Tracking #".

If the shipment gets deleted, it will reverse this process.  Delete the tracking number, update the cost for freight to 0.00.

The problem that I am having is that I have created the trigger.  However, when I apply the trigger and test a package it takes a large amount of time (I've stopped the process at 5 min) to process.

The code of the trigger is below.  Any suggestions:

CREATE TRIGGER ItrgFedExDataHold ON FedExDataHold
FOR INSERT

AS
SET NOCOUNT ON


/* DECLARE Variable  */
DECLARE @TrackingNumber varchar(50)
DECLARE @OrderNumber int
DECLARE @NextLineNumber int
DECLARE @OrderID int
DECLARE @OrderLineID int
DECLARE @FreightLine int
DECLARE @ProductID int
DECLARE @NetCharge money
DECLARE @TotalSellPrice money
DECLARE @TrackingLineID int

DECLARE @curTrackingNumber varchar(50)
DECLARE @curNetCharge money
DECLARE @curCustomerReference varchar(50)
DECLARE @curDeleted varchar(50)
DECLARE @curOrderNumber varchar(25)





/* END DECLARE   */

/* TESTING START*/
--Set Variables for Testing purposes.
--SET @TrackingNumber = '596556153229'
--SET @OrderNumber = 4681340
--SET @NetCharge = 6.65


/* TESTING END */

SET @ProductID = 48019  -- Freight  

/*  Let's Update Freight */

DECLARE curInsert CURSOR FOR
   SELECT TrackingNumber,NetCharge,CustomerReference,deleted
FROM inserted

OPEN curInsert
FETCH NEXT FROM curInsert INTO @curTrackingNumber,@curNetCharge,@curOrderNumber,@curDeleted

WHILE @@FETCH_STATUS = 0


IF LEFT(@curCustomerReference,3) = 'SO-'

/* Sales Order Update  */

BEGIN

    SET @OrderNumber = RIGHT(@curCustomerReference,len(@curCustomerReference)-3)


SET @OrderID = (SELECT OrderID FROM OrderHeader where OrderNumber = @OrderNumber ) --and SaleType

SET @FreightLine = ISNULL((SELECT OrderLineID FROM OrderLine where OrderID = @OrderID and ProductID IN (48019,25917)),0)

SET @NextLineNumber = (SELECT MAX(LineNumber) FROM OrderLine where OrderID = @OrderID)+1

--EXECUTE GetNewID 'OrderLine','OrderLineID',@OrderLineID OUTPUT

BEGIN

/* Let's see if there is a freight line and what the OrderLineID is, if not, 0  */
SET @FreightLine = ISNULL((SELECT OrderLineID FROM OrderLine where OrderID = @OrderID and ProductID IN (48019,25917)),0)

--SELECT @FreightLine

/* Update freight information if there is a freight line   */
IF @FreightLine > 0 and @curDeleted IS NULL  
    BEGIN
    
        SET @TotalSellPrice = (SELECT TotalSellPrice FROM OrderLine where OrderLineID = @FreightLine)
        UPDATE OrderLine
        SET UnitCostPrice = @NetCharge, TotalCostPrice = @NetCharge where OrderLineID = @FreightLine
        
        UPDATE OrderLine
        SET TotalMargin = ROUND((((@TotalSellPrice - @NetCharge)/@TotalSellPrice)*100),2) WHERE OrderLineID = @FreightLine
        
    END

/* Update freight information if there is not an existing freight line   */
IF @FreightLine =  0 and @curDeleted IS NULL
    BEGIN
    
        SET @NextLineNumber = (SELECT MAX(LineNumber) FROM OrderLine where OrderID = @OrderID)+1
        EXECUTE GetNewID 'OrderLine','OrderLineID',@OrderLineID OUTPUT

        INSERT OrderLine(OrderLineID,OrderID,LineNumber,LineType,LineStatus,SaleType,ProductID,UnitCostPrice,TotalCostPrice,UnitSellPrice,TotalMargin,ManualDescription,Quantity,ManualCode,ManualOrderLineTypeID)
        SELECT @OrderLineID,@OrderID,@NextLineNumber,3,7,0,25917,@NetCharge,@NetCharge,0,0,'Freight',1,'Freight',175
        
    END

/*  Let's put in the tracking number   */
IF @curDeleted is NULL

    BEGIN

SET @NextLineNumber = (SELECT MAX(LineNumber) FROM OrderLine where OrderID = @OrderID)+1
EXECUTE GetNewID 'OrderLine','OrderLineID',@OrderLineID OUTPUT

INSERT INTO OrderLine(OrderLineID,OrderID,LineNumber,SaleType,ManualDescription,LineType,LineStatus)
SELECT @OrderLineID,@OrderID,@NextLineNumber,0,('FedEx Tracking: ' + @TrackingNumber),5,0

    END

/*  End of Tracking Number insert   */

IF @curDeleted = '1'
    BEGIN
    
    /* Let's Update the freight line with 0.00 cost and fix the GM% on the ticket.   */
    
    SET @TotalSellPrice = (SELECT TotalSellPrice FROM OrderLine where OrderLineID = @FreightLine)
    UPDATE OrderLine
    SET UnitCostPrice = 0.00, TotalCostPrice = 0.00 WHERE OrderLineID = @FreightLine
    
    UPDATE OrderLine
    SET TotalMargin = ROUND((((@TotalSellPrice) / TotalSellPrice)*100),2) WHERE OrderLineID = @FreightLine
    
    SET @TrackingLineID = (SELECT OrderLineID FROM OrderLine where OrderID = @OrderID and LineType = 5 and ManualDescription like 'FedEx Track%')
    
    DELETE OrderLine
    where OrderLineID = @TrackingLineID     
    
    END




 

END

/* Section for Stock Transfer when we are ready   */

--BEGIN

--END

/* Delete current record from database   */

--BEGIN  

--    DELETE FedExDataHold
--    where TrackingNumber = @curTrackingNumber
    
--END

/*  End Section for tracking number delete   */


FETCH NEXT FROM curInsert INTO @curTrackingNumber,@curNetCharge,@curOrderNumber,@curDeleted

END

CLOSE curInsert

DEALLOCATE curInsert


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>