---
title: "PL/SQL循环语句的使用"
author: "jhlishero"
date: "2011-01-29T14:01:16Z"
tags: ["J#", "SQL"]
categories: ["数据库"]
slug: "pl-sql"
draft: false
lastmod: "2011-01-29T14:01:16Z"
id: 899378
url: "https://jhlishero.iteye.com/blog/899378"
---

**循环语句**

 

```
--1.基本循环
DECLARE
        i INT:=1;
BEGIN
        LOOP     
            EXIT WHEN i=10;
            dbms_output.put_line(i);
            i :=i+1;
        END LOOP;
END;
```

 输出结果：

1  
2  
3  
4  
5  
6  
7  
8  
9

```
--2.WHILE循环
DECLARE
        i int :=1;
BEGIN
        WHILE i<=10 LOOP
              dbms_output.put_line(i);
              i := i+1;
        END LOOP;
END;
```

 

输出结果：

1  
2  
3  
4  
5  
6  
7  
8  
9  
10

 

```
--3.FOR循环
--REVERSE 是可选的
BEGIN
        FOR i IN REVERSE 1..10 LOOP
            dbms_output.put_line(i);
            
        END LOOP;
END;
```

 

输出结果：

10  
9  
8  
7  
6  
5  
4  
3  
2  
1

 

```
--4.嵌套循环和标号
declare
        result int;
begin
       <<outer>>
       for i in 1..100 loop
           <<inter>>
           for j in 1..100 loop
               result := i * j;
               exit outer when result = 1000;
               exit when result = 500;
           end loop inner;
                   
           dbms_output.put_line(result);
       end loop outer;
               
       dbms_output.put_line(result);
end;
```

 

输出结果：

100  
200  
300  
400  
500  
600  
700  
800  
900  
500  
1100  
1200  
1300  
1400  
1500  
1600  
1700  
1800  
1900  
500  
2100  
2200  
2300  
2400  
500  
2600  
2700  
2800  
2900  
3000  
3100  
3200  
3300  
3400  
3500  
3600  
3700  
3800  
3900  
1000