Problem Declaring variables inside a Trigger

  • Hi,

    I am creating a after insert trigger. I am declaring some variables and doing some calculation.

    Attached is the code for regference. I dont know where i am makign a mistake while executing the trigger it is giving following error:

    Msg 207, Level 16, State 1, Procedure tGenerateSalary, Line 33

    Invalid column name '@MonthDays'.

    Msg 207, Level 16, State 1, Procedure tGenerateSalary, Line 33

    Invalid column name '@LWOP'.

    Msg 207, Level 16, State 1, Procedure tGenerateSalary, Line 33

    Invalid column name '@Absent'.

    Msg 207, Level 16, State 1, Procedure tGenerateSalary, Line 33

    Invalid column name '@CasualLeave'.

    Msg 207, Level 16, State 1, Procedure tGenerateSalary, Line 33

    Invalid column name '@CompensatoryLeave'.

    Msg 207, Level 16, State 1, Procedure tGenerateSalary, Line 33

    Invalid column name '@DutyLeave'.

    Msg 207, Level 16, State 1, Procedure tGenerateSalary, Line 33

    Invalid column name '@VacationLeave'.

  • The issue is with the last line of code.

    SET @WorkingDays=(([@MonthDays]-([@LWOP]+[@Absent]*(2)))-((([@CasualLeave]+[@CompensatoryLeave])+[@DutyLeave])+[@VacationLeave]))

    Remove the brackets [ ] around the variable names.

    SET @WorkingDays=((@MonthDays-(@LWOP+@Absent*(2)))-(((@CasualLeave+@CompensatoryLeave)+@DutyLeave)+@VacationLeave))

  • Hi

    You can handle your complete work within one statement:

    DECLARE @WorkingDays decimal(18,0),

    @LWOP decimal(5,2),

    @Absent decimal(5,2),

    @CasualLeave decimal(5,2),

    @CompensatoryLeave decimal (5,2),

    @DutyLeave decimal (5,2),

    @VacationLeave decimal(5,2),

    @MonthDays int

    UPDATE EmployeeSalarySheetNew SET

    @LWOP = ISNULL(LWOP,0)

    ,@Absent = ISNULL(Absent, 0)

    ,@CasualLeave = ISNULL(CasualLeave, 0)

    ,@CompensatoryLeave = ISNULL(CompensatoryLeave, 0)

    ,@DutyLeave = ISNULL(DutyLeave, 0)

    ,@VacationLeave = ISNULL(VacationLeave, 0)

    ,@MonthDays = ISNULL(MonthDays, 0)

    ,BasicAfteMerger= BasicRate

    SET =(SELECT MonthDays FROM EmployeeSalarySheetNew)

    --Calulate WorkingDays

    SET @WorkingDays=((@MonthDays-(@LWOP+@Absent*(2)))-(((@CasualLeave+@CompensatoryLeave)+@DutyLeave)+@VacationLeave))

    BTW: Your last line has no effect since you don't do any further tasks with @WorkingDays

    Greets

    Flo

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply