Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi friends,

Good morning,


I have one doubt ,can we have use for loop in SQL SERVER.
Posted

hi,

SQL server dose not support for loop either you go for While or Do ..While
 
Share this answer
 
 
Share this answer
 
Please using below example you can write ur code.

USE AdventureWorks;
GO
WHILE (
SELECT AVG(ListPrice)
FROM Production.Product) < $300
BEGIN
UPDATE Production.Product
SET ListPrice = ListPrice * 2
SELECT MAX(ListPrice)
FROM Production.Product
IF (
SELECT MAX(ListPrice)
FROM Production.Product) > $500
BREAK
ELSE
CONTINUE
END;
 
Share this answer
 
Please using below example you can write ur code.
SQL
USE AdventureWorks;
GO
WHILE (SELECT AVG(ListPrice) FROM Production.Product) < $300
	BEGIN
          UPDATE Production.Product SET ListPrice = ListPrice * 2
          SELECT MAX(ListPrice)FROM Production.Product
		IF (SELECT MAX(ListPrice) FROM Production.Product) > $500
			BREAK
		ELSE
			CONTINUE 
	END;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900