A cursor is a set of rows together with a pointer that identifies a current row.
In other word,
Here is the syntax with explanation..
DECLARE @fName varchar(50), @lName varchar(50)
In other word,
Cursor
is a database object used by applications to manipulate data in a set on a row-by-row basis, it's like recordset in C#.Here is the syntax with explanation..
DECLARE @fName varchar(50), @lName varchar(50)
DECLARE cursorName CURSOR -- Declare cursor
LOCAL SCROLL STATIC
FOR
Select firstName, lastName FROM myTable
OPEN cursorName -- open the cursor
FETCH NEXT FROM cursorName
INTO @fName, @lName
PRINT @fName + ' ' + @lName -- print the name
WHILE @@FETCH_STATUS = 0
BEGIN
FETCH NEXT FROM cursorName
INTO @fName, @lName
PRINT @fName + ' ' + @lName -- print the name
END
CLOSE cursorName -- close the cursor
DEALLOCATE cursorName -- Deallocate the cursor
Example
BEGIN
declare @EmpName as varchar(20)
declare @WorkDAte as varchar(50)
declare _cur1 cursor for
select distinct WorkDate from EMP_Attendance where FileName=@FileName
--select PunchCardNo from EmployeeInfo where PunchCardNo not in (select PunchCardNo from EMP_Attendance where ) and PunchCardNo<>''
open _cur1;
fetch next from _cur1 into @WorkDAte
--print @WorkDAte
WHILE @@FETCH_STATUS = 0
BEGIN
declare _cur cursor for
--select WorkDate from EMP_Attendance where FileName=@FileName
select EmpName from EmployeeInfo where EmpName not in (select EmpName from EMP_Attendance where WorkDate=@WorkDAte) and EmpName<>''
--print PunchCardNo
open _cur
fetch next from _cur into @EmpName
WHILE @@FETCH_STATUS = 0
--print @PunchCardNo
BEGIN
insert into EMP_Attendance( EmpName, WorkDate, StartTime, EndTime, CreatedBy, CreatedDate, UpdatedBy, UpdatedDate, SummaryId, WorkingTime, latetime, Remarks,FileName )
Select top 1 @EmpName, @WorkDAte, '0', '0', '', '', '', '',3, '', '', '',@FileName from dbo. EMP_Attendance e
where e.EmpName= (select top 1 EmpName from EmployeeInfo where EmpCode='GB0197')
fetch next from _cur into @EmpName
END
DEALLOCATE _cur;
-- CLOSE _cur;
--PRINT @WorkDAte
fetch next from _cur1 into @WorkDAte
END
DEALLOCATE _cur1;
--CLOSE _cur1;
END
Leave your comment
Post a Comment