Sunday, August 31, 2014

Interesting SQL Programming Problem

Another solution you can have is to add an Active bit column. Every time you insert a student that give it the default value of '1' and at the same time UPDATE all this column to '0' for all other rows you have for that student that are '1'. You can do that with a trigger (before insert) or you would need an extra ID row (IDENTITY).


You also need to perform extra logic in case of DELETE but that way you won't need a subquery any more and you can have all the active students with their correct status by:


SELECT * FROM Students WHERE Active = 1


No comments:

Post a Comment