Advice on software / database design to avoid using cursors when updating database
Posted
by Remnant
on Stack Overflow
See other posts from Stack Overflow
or by Remnant
Published on 2010-05-20T10:04:56Z
Indexed on
2010/05/20
10:10 UTC
Read the original article
Hit count: 321
I have a database that logs when an employee has attended a course and when they are next due to attend the course (courses tend to be annual).
As an example, the following employee attended course '1' on 1st Jan 2010 and, as the course is annual, is due to attend next on the 1st Jan 2011. As today is 20th May 2010 the course status reads as 'Complete' i.e. they have done the course and do not need to do it again until next year:
EmployeeID CourseID AttendanceDate DueDate Status
123456 1 01/01/2010 01/01/2011 Complete
In terms of the DueDate
I calculate this in SQL when I update the employee's record e.g. DueDate = AttendanceDate + CourseFrequency (I pull course frequency this from a separate table).
In my web based app (asp.net mvc) I pull back this data for all employees and display it in a grid like format for HR managers to review. This allows HR to work out who needs to go on courses.
The issue I have is as follows.
Taking the example above, suppose today is 2nd Jan 2011. In this case, employee 123456 is now overdue for the course and I would like to set the Status
to Incomplete so that the HR manager can see that they need to action this i.e. get employee on the course.
I could build a trigger in the database to run overnight to update the Status
field for all employees based on the current date. From what I have read I would need to use cursors to loop over each row to amend the status and this is considered bad practice / inefficient or at least something to avoid if you can???
Alternatively, I could compute the Status
in my C# code after I have pulled back the data from the database and before I display it on screen. The issue with this is that the Status
in the database would not necessarily match what is shown on screen which just feels plain wrong to me.
Does anybody have any advice on the best practice approach to such an issue?
It helps, if I did use a cursor I doubt I would be looping over more than 1000 records at any given time. Maybe this is such small volume that using cursors is okay?
© Stack Overflow or respective owner