Loop in Sql Server query

57
Views
1
Answers

Hi, How to use Loops in Sql Server queries?

edit | flag
William Patrick
Asked on: Jun 28, 2011 at 12:57AM

1 Answers

42
7
42

WHILE statement sets a condition for the repeated execution of an SQL statement or statement block. Following is very simple example of WHILE Loop with BREAK and CONTINUE.

USE AdventureWorks;
GO
DECLARE
@Flag INT
SET
@Flag = 1
WHILE
(@Flag < 10)
BEGIN
BEGIN
PRINT
@Flag
SET
@Flag = @Flag + 1
END
IF
(@Flag > 5)
BREAK
ELSE
CONTINUE
END
edit | flag
Gokul A
Answered on: Jun 28, 2011 at 1:09AM

Post your Answer

Search

Welcome to Ask Amoeba!
This is 100% free and interactive site for sharing professional Questions and Answers, Opensource projects, Interview questions.
Learners, beginners, Experts stop and share your knowledge and ideas!

Browse Categories

Browse Tags