mercredi 16 décembre 2015

Print is not working for some values in SQL

I am stucked here for one small reason.

I want to display the user some error message based on some condition. and it is below

IF user selects LeaveType as OL then its fine, but if User selects LeaveType as PL it should print Error.

Because, while selecting PL first the @OL_FirstHalf remains 0 and there will no rows gets affected.

The rows will get affected if @OL_FirstHalf = 1

So what I tried is below:-

IF (@OLType = 'OL')
     BEGIN
           INSERT INTO p_leave_allocation_14122015(Mkey,Entry_Sr_no,Emp_doj, Comp_mkey,FDepartment_Id,FModule_Id,Month,Year, Emp_mkey, Emp_card_no,OL_FirstHalf)SELECT ROW_NUMBER() OVER(ORDER BY lev.MKey) + 1643,ROW_NUMBER() OVER(ORDER BY Entry_Sr_no) + 898, emp.Date_Of_Joining, lev.Comp_mkey,FDepartment_Id, FModule_Id,1,@Year,Emp_mkey,lev.Emp_card_no,1) FROM p_leave_allocation lev LEFT JOIN Emp_mst emp
                 ON Emp.Emp_card_no = lev.Emp_card_no and lev.Year = (@Year -1)
            WHERE emp.Status in ('A', 'S')  and 
                  YEAR(emp.Date_Of_Joining) <= @Year
 ELSE IF (@OLType = 'PL')
        IF(@OL_FirstHalf = 0)
        BEGIN
            Print 'Error'
        END 

But it is giving me error as

Must declare the scalar variable "@OL_FirstHalf".

I am using sql-server-2005. Sorry I couldn't format the code.

Please suggest what to do ?

Aucun commentaire:

Enregistrer un commentaire