How to write sql query efficiently in this case? -
I have a table that lists the students and their rank.
Similarly, in my UI client side, I show a list of all the students in the list list, sorted by their rank.
Code> [student name, student-rank]
Now this user has a professor drop and re-order Students can choose, and then "saves"
is left, I need to get the list from the listbox and then make an update on the table so that the students rank according to the list in the UI Got it
To do this, I'm just fixing the easiest way, to delete all the previous rows and then after each of the student tables, insert the rank one by one after adding each one. I think it is unnecessary .... but I'm not sure.
I was wondering if there is a better way of achieving this?
If you are worried about reducing database changes, you can track that line In which the change has happened and only updates for those people if it is appropriate or depends on the scale of your data and how much, if any, you have to enter. For example, if you have ten students and swap rank 5 and 6, you only affect the minimum update with 2 rows with your default drop and 10 required to remove and 10 inserts. If you in turn take a student from Rank 8 to Rank 5 then you have more updates, but still less:
UPDATE student ST rank = rank + 1 where ranking Position Between 5 and 7 SIT Rank = 5 Where Name = @name
But the optimal SQL implementation is complex and is probably not worth the effort. Perhaps it is best to update the rows.
If you delete and re-insert, then you want to TRUNCATE instead of DELETE and make sure that you have removed the transaction in order to ensure that the insert and fail fails.
Comments
Post a Comment