Here we explain how and when to use While Loops in SQL Server and why they are sometimes beneficial over your standard Set Based Logic
Set Based Logic
Set based logic effectively takes a set of data and attempts to update it, the problem with that is if the set of data is huge then this is going to use a lot of server resource such as memory, threads etc. It will also put a lock on the table preventing other users from accessing the data - this can be very problematic.
Set based logic has it's place, and if anything SQL Server is designed around set based logic but there is a fine line between efficiency and poor performance.
When we have a huge number of rows to update then it's beneficial to take a small subset of those and update them in batches. Think of it as multiple loads of Set based logic.
This is where While Loops come in - or sometimes cursors.
While Loops
These are sometimes called Do While Loops, but essentially they take a small amount of the data and update, delete, alter it then go and do the next batch.
Your job is to code the loop to ensure it doesn't run continuously and to make sure it doesn't get the same number of rows more than once. it's pretty simple once you get the hang of it!
Content
00:00 Lesson overview
00:50 Set Based Logic
01:46 How to use a While Loop
06:00 Setting your counter for performance
Auf dieser Seite können Sie das Online-Video SQL While Loops Made Easy! | When you to use a While Loop and when Not To! | SQL Server Tutorial mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer The SQL Guy 26 November 2024 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 2,616 Mal angesehen und es wurde von 11 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!