Previous: SQL Server IF Conditions. Looping structures allow a single command, or a group of statements, to be executed repeatedly. When using the T-SQL WHILE loop, a Boolean condition is checked every time the code within the loop is about to start. If the condition is true, the loop is executed.

In this chapter, we will discuss Loops in PL/SQL. There may be a situation when you need to execute a block of code several number of times. In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on.

Programming languages provide various control structures that allow for more complicated execution paths.

A loop statement allows us to execute a statement or group of statements multiple times and following is the general form of a loop statement in most of the programming languages −

PL/SQL provides the following types of loop to handle the looping requirements. Click the following links to check their detail.

S.NoLoop Type & Description
1PL/SQL Basic LOOP

In this loop structure, sequence of statements is enclosed between the LOOP and the END LOOP statements. At each iteration, the sequence of statements is executed and then control resumes at the top of the loop.

2PL/SQL WHILE LOOP

Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.

3PL/SQL FOR LOOP

Execute a sequence of statements multiple times and abbreviates the code that manages the loop variable.

4Nested loops in PL/SQL

You can use one or more loop inside any another basic loop, while, or for loop.

Labeling a PL/SQL Loop

PL/SQL loops can be labeled. The label should be enclosed by double angle brackets (<< and >>) and appear at the beginning of the LOOP statement. The label name can also appear at the end of the LOOP statement. You may use the label in the EXIT statement to exit from the loop.

The following program illustrates the concept −

When the above code is executed at the SQL prompt, it produces the following result −

The Loop Control Statements

Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed.

PL/SQL supports the following control statements. Labeling loops also help in taking the control outside a loop. Click the following links to check their details.

While Loop In Sql Server 2008 Function

S.NoControl Statement & Description
1EXIT statement

The Exit statement completes the loop and control passes to the statement immediately after the END LOOP.

2CONTINUE statement

Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.

3GOTO statement

Transfers control to the labeled statement. Though it is not advised to use the GOTO statement in your program.

I am trying to execute this code below. It is a simplified example of the actual code I have to make, so I know that it is useless to loop in such a way. However, I need to look and union select statements in SQL Server. When I try to run this query I get an error:

Incorrect syntax near the keyword 'END'.

While Loop In Function Sql Server

Any ideas?

marc_s
595k135 gold badges1139 silver badges1280 bronze badges
richsonirichsoni
1,8017 gold badges24 silver badges40 bronze badges

closed as too localized by LittleBobbyTables, marc_s, Martin Smith, Stuart Ainsworth, Ash BurlaczenkoOct 13 '12 at 21:23

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. If this question can be reworded to fit the rules in the help center, please edit the question.

4 Answers

You need a second set to UNION on. The while loop doesn't hold on to the selected set and know it the next time through. What you're trying to accomplish is basically select all the data from mytable then union it again on the same set...n times. Which really doesn't make sense. Remember that SQL is set based, so select * From mytable will select every record in there. You also never state what you're trying to accomplish.

Samir BanjanovicSamir Banjanovic
3061 gold badge3 silver badges16 bronze badges
Tutorial

That query makes no sense. To use UNION, you need to be selecting from a second table.

SQL Server essentially sees:

With no second table after the UNION.

LittleBobbyTablesLittleBobbyTables
27.5k12 gold badges89 silver badges105 bronze badges

Instead of trying to use UNION I would use a temp table or temp table variable to merge the result sets

Sql While Loop Counter

msmucker0527msmucker0527
4,4031 gold badge18 silver badges35 bronze badges

While Loop In Function Sql Server Free

This way gets rid of the the UNION statement and avoids a loop altogether:

Stuart AinsworthStuart Ainsworth

Not the answer you're looking for? Browse other questions tagged sqlsql-server or ask your own question.