Avatar of Adam Davis
Adam Davis

asked on 

When to use a While loop

Hi


Can someone please explain to me when I would need to use an while loop. I understand the principles of the while loop but not sure when I would actually need to use one?

Thank you
Microsoft SQL Server

Avatar of undefined
Last Comment
Olaf Doschke
SOLUTION
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
ASKER CERTIFIED SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
SOLUTION
Avatar of Olaf Doschke
Olaf Doschke
Flag of Germany image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Olaf Doschke
Olaf Doschke
Flag of Germany image

Vitor is right.

An even simpler answer: When you google "T-SQL WHILE" you'll not only find the definition, you'll find code of people using it. You can see for what they use it. ;)

I just did that and found no other uses than for cursor on the first few result pages. I'll give another one here related to a recent question. The following batch processing of a DELETE can lower the stress on the transaction log:

Declare @RowCount int;
Set @RowCount=1;
SET ROWCOUNT 1000;
WHILE @RowCount>0
BEGIN 
    BEGIN TRAN;
    DELETE FROM Table WHERE somedate<'20160101'    
    SET @RowCount = @@ROWCOUNT;
    COMMIT TRAN;
    CHECKPOINT;
END

Open in new window

Especially if the DELETE would otherwise delete millions of rows or causes cascading deletes, the processing of the DELETE command unchanged but limited by SET ROWCOUNT in a WHILE loop can help to have an overall better performance and preventing growth of the transaction log without needing to write a WHERE clause with changing range of dates or IDs.

Bye, Olaf.
Avatar of Adam Davis
Adam Davis

ASKER

Thank you all for your help
Avatar of Zberteoc
Zberteoc
Flag of Canada image

@Olaf

That technique with SET ROWCOUNT 1000 is something new to me and it is awesome!. Thanks for sharing !
Avatar of Zberteoc
Zberteoc
Flag of Canada image

oops, I was a bit too soon enthusiastic:

Microsoft:

Using SET ROWCOUNT will not affect DELETE, INSERT, and UPDATE statements in a future release of SQL Server. Avoid using SET ROWCOUNT with DELETE, INSERT, and UPDATE statements in new development work, and plan to modify applications that currently use it. For a similar behavior, use the TOP syntax. For more information, see TOP (Transact-SQL).

So it stopped with SQL 2008.
Avatar of Olaf Doschke
Olaf Doschke
Flag of Germany image

Well, that's new to me, thank YOU for pointing that out.

Bye, Olaf.

Edit: While the deprecation already was announced in the SQL2008 topic, the version really deprecating this feature is the future version following SQL2016, see https://msdn.microsoft.com/en-us/library/ms143729.aspx

Edit 2: I actually tried with the Microsoft SQL Server 2016 (RC3) - 13.0.1400.361 (X64) and SET ROWCOUNT still is in effect on DELETE.
As the list says it only will deprecate in the next version. Anyway it's easy to do the same with a TOP (1000) clause.
Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo