Hi All ,
I am trying to use a cursor and its showing error The variable does not currently have a cursor allocated to it even though its allocated . Any help will be really appreciated . Thanks in advance .
What I have tried:
<pre>DECLARE @firstday1 Date = null,
@Lastday1 Date = null,
@RequestType1 nvarchar = null,
@EmployeeIdRequest1 int = null
SELECT @firstday1 = FORMAT(PeriodFrom,'yyyy-MM-dd') , @Lastday1 = FORMAT(PeriodTo,'yyyy-MM-dd') ,@RequestType1 = TRIM(RequestType),@EmployeeIdRequest1 = EmployeeID FROM MST_TBL_Approvalrequests WHERE Autoid = @ApprovalReqID;
DECLARE @Enumerator1 CURSOR
SET @Enumerator1 = CURSOR FAST_FORWARD FOR
with dates_CTE1 (date) as (
select @firstday1
Union ALL
select DATEADD(day, 1, date)
from dates_CTE1
where date < @Lastday1
)
select date FROM dates_CTE1
OPTION (maxrecursion 365);
OPEN @Enumerator1
DECLARE @LoopDate1 date
WHILE (1=1)
BEGIN
FETCH NEXT FROM @Enumerator1 into @LoopDate1
IF (@@FETCH_STATUS <> 0) break
BEGIN
if exists(SELECT AutoId from PPMS_TBL_AttendanceMaster where FORMAT(date,'yyyy-MM-dd')=@LoopDate1 and EmployeeID = @EmployeeIdRequest1 )
BEGIN
UPDATE PPA SET
PPA.PresentStatus = case when (MSTA.RequestTypeID = 10 and PPA.PresentStatus ='Work From Home') then 'Work From Home'
when (MSTA.RequestTypeID = 10 and PPA.PresentStatus NOT IN ('Work From Home')) THEN
case when ((CAST(PPA.AdjustOuttime AS TIME)) < (CAST(TSH.ShiftEndTime AS TIME))) then 'Half Day' else 'Present' end
when MSTA.RequestTypeID = 12 then 'Present' else MSTA.RequestType end
,PPA.IsApproved=1
,PPA.InTime = case when MSTA.RequestTypeID = 10 then PPA.AdjustInTime else InTime end
,PPA.OutTime= case when MSTA.RequestTypeID = 10 then PPA.AdjustOuttime else OutTime end
,StatusAlert = case when MSTA.RequestTypeID = 10 then (CASE WHEN (((-1) * DATEDIFF(MINUTE, TSH.ShiftStartTime, CONVERT(VARCHAR(8),PPA.AdjustInTime,108))) >= 0) THEN 'Green' ELSE 'Red' END) else StatusAlert end
,LateHours = case when MSTA.RequestTypeID = 10 then (CASE WHEN ((-1) * DATEDIFF(MINUTE, TSH.ShiftStartTime, CONVERT(VARCHAR(8),PPA.AdjustInTime,108)) < 0) THEN (DATEDIFF(MINUTE, TSH.ShiftStartTime, CONVERT(VARCHAR(8),PPA.AdjustInTime,108))) ELSE '' END) else LateHours end
from MST_TBL_Approvalrequests MSTA
INNER JOIN PPMS_TBL_AttendanceMaster PPA ON MSTA.EmployeeID = PPA.EmployeeID
INNER JOIN PPMS_TBL_EmployeeMaster PTE ON PPA.EmployeeID = PTE.Autoid
left JOIN TBL_Shifts TSH ON TSH.Autoid=PTE.ShiftCatagoryID
WHERE ApprovedStatus = 'Approved' and MSTA.Autoid = @ApprovalReqID
and (date BETWEEN PeriodFrom AND PeriodTo) and MSTA.EmployeeID = @EmployeeIdRequest1 AND RequestTypeID in (1,2,3,4,5,6,7,8,9,10)
END
ELSE
BEGIN
INSERT INTO [PPMS_TBL_AttendanceMaster](EmployeeID, Name, [Date], AttendanceStatus,PresentStatus)
SELECT AutoID,Name, @LoopDate1,'Waiting for Review',@RequestType
FROM [PPMS_TBL_EmployeeMaster] where AutoID = @EmployeeIdRequest1
and PPMS_TBL_EmployeeMaster.active=1
END
end
CLOSE @Enumerator1
DEALLOCATE @Enumerator1