插入记录

时间:2016-11-13 21:23:49

标签: c# asp.net sql-server triggers

我有这个代码成功地在LABOR_TICKET表中插入记录,但我不完全了解触发器,我无法弄清楚为什么触发器的一部分无法更新表操作

我的代码:

protected void btnSubmit_Click(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection("Data Source=infor2;Initial Catalog=xxxxxx;Integrated Security=True");

    SqlCommand insert = new SqlCommand(@"insert into labor_ticket
                                                        (TRANSACTION_ID,
                                                        WORKORDER_BASE_ID,
                                                        OPERATION_SEQ_NO,
                                                        TYPE,
                                                        SETUP_COMPLETED,
                                                        EMPLOYEE_ID,
                                                        DEPARTMENT_ID,
                                                        RESOURCE_ID,
                                                        HOURS_WORKED,
                                                        MULTIPLIER_1,
                                                        MULTIPLIER_2,
                                                        GOOD_QTY,
                                                        BAD_QTY,
                                                        TRANSACTION_DATE,
                                                        POSTING_CANDIDATE,
                                                        INDIRECT_CODE,
                                                        ACT_LABOR_COST,
                                                        ACT_BURDEN_COST,
                                                        CREATE_DATE)
                                                        VALUES(@TRANSACTION_ID,
                                                        @WORKORDER_BASE_ID,
                                                        @OPERATION_SEQ_NO,
                                                        @TYPE,
                                                        @SETUP_COMPLETED,
                                                        @EMPLOYEE_ID,
                                                        @DEPARTMENT_ID,
                                                        @RESOURCE_ID,
                                                        @HOURS_WORKED,
                                                        @MULTIPLIER_1,
                                                        @MULTIPLIER_2,
                                                        @GOOD_QTY,
                                                        @BAD_QTY,
                                                        @TRANSACTION_DATE,
                                                        @POSTING_CANDIDATE,
                                                        @INDIRECT_CODE,
                                                        @ACT_LABOR_COST,
                                                        @ACT_BURDEN_COST,
                                                        @CREATE_DATE)", conn);

    insert.Parameters.AddWithValue("@TRANSACTION_ID", 78535);
    insert.Parameters.AddWithValue("@WORKORDER_BASE_ID", "W14935");
    insert.Parameters.AddWithValue("@OPERATION_SEQ_NO", 20);
    insert.Parameters.AddWithValue("@TYPE", "R");
    insert.Parameters.AddWithValue("@SETUP_COMPLETED", "Y");
    insert.Parameters.AddWithValue("@EMPLOYEE_ID", "EZ0022");
    insert.Parameters.AddWithValue("@DEPARTMENT_ID", "Operations");
    insert.Parameters.AddWithValue("@RESOURCE_ID", "WC0VIBRA");
    insert.Parameters.AddWithValue("@HOURS_WORKED", 1.50);
    insert.Parameters.AddWithValue("@MULTIPLIER_1", 1.00);
    insert.Parameters.AddWithValue("@MULTIPLIER_2", 1.00);
    insert.Parameters.AddWithValue("@GOOD_QTY", 7.0);
    insert.Parameters.AddWithValue("@BAD_QTY", 0.0000);
    insert.Parameters.AddWithValue("@TRANSACTION_DATE", "2016-11-04 01:00:00.000");
    insert.Parameters.AddWithValue("@POSTING_CANDIDATE", "Y");
    insert.Parameters.AddWithValue("@INDIRECT_CODE", "X");
    insert.Parameters.AddWithValue("@ACT_LABOR_COST", 1.00);
    insert.Parameters.AddWithValue("@ACT_BURDEN_COST", 1.00);
    insert.Parameters.AddWithValue("@CREATE_DATE", "2016-11-04 02:30:00.000");

    try
    {
            conn.Open();
            insert.ExecuteNonQuery();
            Response.Write("labor ticket record inserted !");
    }
    catch (Exception ex)
    {
            Response.Write("Error: " + ex.Message);
            conn.Close();
    }
}

这是与LABOR_TICKET表关联的触发器:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO

ALTER TRIGGER [dbo].[INSERT_LABOR_TICKET] 
ON [dbo].[LABOR_TICKET] 
FOR INSERT 
AS
    SET NOCOUNT ON  

    DECLARE @nRcd INT  
    DECLARE @CostingStdLabor CHAR  
    DECLARE @CostingMethod CHAR  
    DECLARE @TYPE CHAR  
    DECLARE @MULTIPLIER_1 DEC(5,3)  
    DECLARE @MULTIPLIER_2 DEC(5,3)  
    DECLARE @POSTING_CANDIDATE CHAR  
    DECLARE @SETUP_COMPLETED CHAR  
    DECLARE @TRANSACTION_ID INT  
    DECLARE @WORKORDER_TYPE CHAR  
    DECLARE @WORKORDER_BASE_ID VARCHAR(30)  
    DECLARE @WORKORDER_LOT_ID VARCHAR(3)  
    DECLARE @WORKORDER_SPLIT_ID VARCHAR(3)  
    DECLARE @WORKORDER_SUB_ID VARCHAR(3)  
    DECLARE @OPERATION_SEQ_NO SMALLINT  
    DECLARE @CALC_END_QTY DEC(14,4)  
    DECLARE @COMPLETED_QTY DEC(14,4)  
    DECLARE @EST_LABOR_COST DEC(15,2)  
    DECLARE @EST_BURDEN_COST DEC(15,2)  
    DECLARE @STATUS CHAR  
    DECLARE @CLOSE_DATE DATETIME  
    DECLARE @NewCompletedQty DEC(14,4)  
    DECLARE @QtyOver DEC(14,4)  
    DECLARE @GOOD_QTY DEC(14,4)  
    DECLARE @Ratio1 DEC(14,4)  
    DECLARE @Ratio2 DEC(14,4)  
    DECLARE @TotalLaborCost DEC(15,2)  
    DECLARE @TotalBurdenCost DEC(15,2)  
    DECLARE @LaborCost DEC(15,2)  
    DECLARE @BurdenCost DEC(15,2)  
    DECLARE @HOURS_WORKED DEC(7,2)  
    DECLARE @HOURLY_COST DEC(15,2)  
    DECLARE @BAD_QTY DEC(14,4)  
    DECLARE @UNIT_COST DEC(15,6)  
    DECLARE @BURDEN_PERCENT DEC(6,3)  
    DECLARE @BURDEN_PER_HR DEC(15,2)  
    DECLARE @BURDEN_PER_UNIT DEC(15,6)  
    DECLARE @BUR_PER_OPERATION DEC(15,2)  
    DECLARE @ACT_LABOR_COST DEC(15,2)  
    DECLARE @ACT_BURDEN_COST DEC(15,2)  
    DECLARE @SetupHrs DEC(7,2)  
    DECLARE @RunHrs DEC(7,2)  
    DECLARE @TRANSACTION_DATE DATETIME  
    SELECT @nRcd = 0  

    DECLARE LABOR_TICKET_INS CURSOR LOCAL FOR  
     SELECT  
     TRANSACTION_ID,  
     TYPE, MULTIPLIER_1, MULTIPLIER_2, POSTING_CANDIDATE, SETUP_COMPLETED, ISNULL(HOURS_WORKED, 0),  
     WORKORDER_TYPE, WORKORDER_BASE_ID, WORKORDER_LOT_ID, WORKORDER_SPLIT_ID, WORKORDER_SUB_ID, OPERATION_SEQ_NO,  
     ISNULL(GOOD_QTY, 0), ISNULL(HOURLY_COST, 0), ISNULL(BAD_QTY, 0), ISNULL(UNIT_COST, 0),  
     ISNULL(BURDEN_PERCENT, 0), ISNULL(BURDEN_PER_HR, 0), ISNULL(BURDEN_PER_UNIT, 0),  
     ISNULL(BUR_PER_OPERATION,0), TRANSACTION_DATE  
     FROM INSERTED  
    OPEN LABOR_TICKET_INS  
    FETCH LABOR_TICKET_INS INTO  
     @TRANSACTION_ID,  
     @TYPE, @MULTIPLIER_1, @MULTIPLIER_2, @POSTING_CANDIDATE, @SETUP_COMPLETED, @HOURS_WORKED,  
     @WORKORDER_TYPE, @WORKORDER_BASE_ID, @WORKORDER_LOT_ID, @WORKORDER_SPLIT_ID, @WORKORDER_SUB_ID, @OPERATION_SEQ_NO,  
     @GOOD_QTY, @HOURLY_COST, @BAD_QTY, @UNIT_COST,  
     @BURDEN_PERCENT, @BURDEN_PER_HR, @BURDEN_PER_UNIT, @BUR_PER_OPERATION, @TRANSACTION_DATE  
    WHILE (@nRcd = 0 and @@FETCH_STATUS <> -1)  
    BEGIN  
     select @CostingStdLabor = costing_std_labor, @CostingMethod = costing_method from APPLICATION_GLOBAL  
     select  
     @CALC_END_QTY = calc_end_qty,  
     @COMPLETED_QTY = completed_qty,  
     @EST_LABOR_COST = est_atl_lab_cost,  
     @EST_BURDEN_COST = est_atl_bur_cost,  
     @STATUS = status,  
     @CLOSE_DATE = close_date  
     from operation  
     where  
     workorder_type = @WORKORDER_TYPE  
     and workorder_base_id = @WORKORDER_BASE_ID  
     and workorder_lot_id = @WORKORDER_LOT_ID  
     and workorder_split_id = @WORKORDER_SPLIT_ID  
     and workorder_sub_id = @WORKORDER_SUB_ID  
     and sequence_no = @OPERATION_SEQ_NO  
     SELECT @NewCompletedQty = @COMPLETED_QTY + @GOOD_QTY  
     SELECT @QtyOver = @NewCompletedQty - @COMPLETED_QTY  
     If @CostingMethod = 'S' And @CostingStdLabor = 'Q' And @TYPE != 'I'  
     BEGIN  
     SELECT @Ratio1 = 0  
     SELECT @Ratio2 = 0  
     If @CALC_END_QTY != 0  
     BEGIN  
     SELECT @Ratio1 = @NewCompletedQty / @CALC_END_QTY  
     SELECT @Ratio2 = @QtyOver / @CALC_END_QTY  
     END  
     SELECT @TotalLaborCost = @EST_LABOR_COST * @Ratio1  
     SELECT @TotalBurdenCost = @EST_BURDEN_COST * @Ratio1  
     SELECT @LaborCost = @EST_LABOR_COST * @Ratio2  
     SELECT @BurdenCost = @EST_BURDEN_COST * @Ratio2  
     END  
     Else  
     BEGIN  
     SELECT @LaborCost = (@HOURS_WORKED * @HOURLY_COST * @MULTIPLIER_1 * @MULTIPLIER_2) + ((@GOOD_QTY + @BAD_QTY) * @UNIT_COST)  
     SELECT @BurdenCost = (@LaborCost * (@BURDEN_PERCENT/100)) + (@HOURS_WORKED * @BURDEN_PER_HR) + ((@GOOD_QTY + @BAD_QTY) * @BURDEN_PER_UNIT) + @BUR_PER_OPERATION  
     END  
     If (@TYPE = 'I')  
     SELECT @POSTING_CANDIDATE = 'Y'  
     SELECT @ACT_LABOR_COST = @LaborCost + .005  
     SELECT @ACT_BURDEN_COST = @BurdenCost + .005  
     UPDATE LABOR_TICKET SET  
     POSTING_CANDIDATE = @POSTING_CANDIDATE,  
     ACT_LABOR_COST = @LaborCost,  
     ACT_BURDEN_COST = @BurdenCost  
     WHERE TRANSACTION_ID = @TRANSACTION_ID  
     If @TYPE != 'I'  
     BEGIN  
     If @TYPE = 'R'  
     BEGIN  
     SELECT @SETUP_COMPLETED = 'Y'  
     SELECT @SetupHrs = 0  
     SELECT @RunHrs = @HOURS_WORKED  
     END  
     Else  
     BEGIN  
     SELECT @SetupHrs = @HOURS_WORKED  
     SELECT @RunHrs = 0  
     END  
     If @NewCompletedQty >= @CALC_END_QTY  
     BEGIN  
     SELECT @STATUS = 'C'  
     SELECT @CLOSE_DATE = @TRANSACTION_DATE  
     END  
     If @CostingMethod = 'S' And @CostingStdLabor = 'Q'  
     update OPERATION set  
     act_atl_lab_cost = round( @TotalLaborCost, 2 ),  
     act_atl_bur_cost = round( @TotalBurdenCost, 2 ),  
     completed_qty = completed_qty + @GOOD_QTY,  
     deviated_qty = deviated_qty + @BAD_QTY,  
     act_setup_hrs = act_setup_hrs + @SetupHrs,  
     act_run_hrs = act_run_hrs + @RunHrs,  
     setup_completed = @SETUP_COMPLETED,  
     status = @STATUS,  
     close_date = @CLOSE_DATE  
     where  
     workorder_type = @WORKORDER_TYPE  
     and workorder_base_id = @WORKORDER_BASE_ID  
     and workorder_lot_id = @WORKORDER_LOT_ID  
     and workorder_split_id = @WORKORDER_SPLIT_ID  
     and workorder_sub_id = @WORKORDER_SUB_ID  
     and sequence_no = @OPERATION_SEQ_NO  
     Else  
     update OPERATION set  
     act_atl_lab_cost = act_atl_lab_cost + round(@LaborCost, 2),  
     act_atl_bur_cost = act_atl_bur_cost + round(@BurdenCost, 2),  
     completed_qty = completed_qty + @GOOD_QTY,  
     deviated_qty = deviated_qty + @BAD_QTY,  
     act_setup_hrs = act_setup_hrs + @SetupHrs,  
     act_run_hrs = act_run_hrs + @RunHrs,  
     setup_completed = @SETUP_COMPLETED,  
     status = @STATUS,  
     close_date = @CLOSE_DATE  
     where  
     workorder_type = @WORKORDER_TYPE  
     and workorder_base_id = @WORKORDER_BASE_ID  
     and workorder_lot_id = @WORKORDER_LOT_ID  
     and workorder_split_id = @WORKORDER_SPLIT_ID  
     and workorder_sub_id = @WORKORDER_SUB_ID  
     and sequence_no = @OPERATION_SEQ_NO  
     END  
     UPDATE WORK_ORDER SET DESIRED_QTY = DESIRED_QTY * 1 WHERE type = @WORKORDER_TYPE  
     and base_id = @WORKORDER_BASE_ID  
     and lot_id = @WORKORDER_LOT_ID  
     and split_id = @WORKORDER_SPLIT_ID  
     and sub_id = '0'  
     update EQUIP_PM_SCHEDULE set INVOICE_FLAG = 'R'  
     where  
     workorder_type = @WORKORDER_TYPE  
     AND workorder_base_id = @WORKORDER_BASE_ID  
     AND workorder_lot_id = @WORKORDER_LOT_ID  
     AND workorder_split_id = @WORKORDER_SPLIT_ID  
     AND workorder_sub_id = @WORKORDER_SUB_ID  
     AND cust_order_id is not NULL  
     update UNP_MNT_CALLS set INVOICE_FLAG = 'R'  
     where  
     workorder_type = @WORKORDER_TYPE  
     AND workorder_base_id = @WORKORDER_BASE_ID  
     AND workorder_lot_id = @WORKORDER_LOT_ID  
     AND workorder_split_id = @WORKORDER_SPLIT_ID  
     AND workorder_sub_id = @WORKORDER_SUB_ID  
     AND cust_order_id is not NULL  
     FETCH LABOR_TICKET_INS INTO  
     @TRANSACTION_ID,  
     @TYPE, @MULTIPLIER_1, @MULTIPLIER_2, @POSTING_CANDIDATE, @SETUP_COMPLETED, @HOURS_WORKED,  
     @WORKORDER_TYPE, @WORKORDER_BASE_ID, @WORKORDER_LOT_ID, @WORKORDER_SPLIT_ID, @WORKORDER_SUB_ID, @OPERATION_SEQ_NO,  
     @GOOD_QTY, @HOURLY_COST, @BAD_QTY, @UNIT_COST,  
     @BURDEN_PERCENT, @BURDEN_PER_HR, @BURDEN_PER_UNIT, @BUR_PER_OPERATION, @TRANSACTION_DATE  
    END  
    DEALLOCATE LABOR_TICKET_INS  
    IF (@nRcd <> 0) RAISERROR('VMFG-%d error in trigger INSERT_LABOR_TICKET', 16, -1, @nRcd)  
    IF (@nRcd <> 0 Or @@ERROR <> 0) ROLLBACK TRANSACTION

这个触发器的部分(从第154行到第172行)我无法弄清楚为什么它没有被执行我的逻辑认为它应该执行的地方因为触发器的第136行上的这个语句被评估为是假的:

第154行 - 第172行:

 Else  
 update OPERATION set  
 act_atl_lab_cost = act_atl_lab_cost + round(@LaborCost, 2),  
 act_atl_bur_cost = act_atl_bur_cost + round(@BurdenCost, 2),  
 completed_qty = completed_qty + @GOOD_QTY,  
 deviated_qty = deviated_qty + @BAD_QTY,  
 act_setup_hrs = act_setup_hrs + @SetupHrs,  
 act_run_hrs = act_run_hrs + @RunHrs,  
 setup_completed = @SETUP_COMPLETED,  
 status = @STATUS,  
 close_date = @CLOSE_DATE  
 where  
 workorder_type = @WORKORDER_TYPE  
 and workorder_base_id = @WORKORDER_BASE_ID  
 and workorder_lot_id = @WORKORDER_LOT_ID  
 and workorder_split_id = @WORKORDER_SPLIT_ID  
 and workorder_sub_id = @WORKORDER_SUB_ID  
 and sequence_no = @OPERATION_SEQ_NO  
 END  

0 个答案:

没有答案