Skip to content
Advertisement

To convert the WHILE loop query into FOR loop query

This is the table created and inserted values into it in SQL server

And using WHILE loop, I achieved the below query in SQL Server.

Now I want the same above result with the usage of FOR loop, I know in SQL server FOR loop is not used, but with WHILE loop we can simulate the FOR loop in SQL server.

Can anyone help me with this?

Advertisement

Answer

Use a query, not a loop with print:

In SQL we always try to work on sets at once, and not use while loops or cursors.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement