Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can't execute a while clause in SQL Server with Dbeaver

I am trying to execute a loop while in Dbeaver with SQL Server, but the statement keeps loading and does not execute it. I just want to print the word ‘ok!’ 3 times.

I’m not sure if it’s a loop problem, a Dbeaver problem, or another.

Can anyone help please?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

My code:

DECLARE @cnt INT = 0;

WHILE @cnt < 3
BEGIN
   PRINT 'ok!';
   
END;

Screenshot from Dbeaver

>Solution :

@cnt never increments, so this loop will never finish. It will never yield control back to the system to let it even show the first ok string. You need to add this:

DECLARE @cnt INT = 0;

WHILE @cnt < 3
BEGIN
   PRINT 'ok!';
   Set @cnt = @cnt + 1;
END;

Anyway, 99 times out of a 100, if you’re writing a loop in SQL at all you’re doing something very wrong. SQL really wants to operating on whole sets at a time, not individual items in the sets via loops.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading