search
HomeDatabaseMysql Tutorial常用SQL/oracle循环语句
常用SQL/oracle循环语句Jun 07, 2016 pm 03:44 PM
oraclesqluseCommonly usedcyclestatement

2、使用T-SQL标准控制结构: 1 定义语句块 2 IF ... ELSE语句 3 IF EXISTS语句 语法: declare select @lname = ‘Smith’ if exists(select * from titles where au_lname = @lname) begin select @msg = ‘There are authors named’ @lname print @msg en

 

2、使用T-SQL标准控制结构:

       1> 定义语句块

       2> IF ... ELSE语句 

       3> IF EXISTS语句

          语法:

              declare              select @lname = ‘Smith’

              if exists(select * from titles where au_lname = @lname)

                 begin

                    select @msg = ‘There are authors named’ + @lname

                    print @msg

                 end

      4> 循环语句:

          示例:

              while @avg_price

              begin

                 select @avg_price          = avg(price) * 1.05,

                        @max_price          = max(price) * 1.05,

                        @time_thru_the_loop = @time_thru_the_loop + 1

              end

              if @time_thru_the_loop = 0

                 select @time_thru_the_loop = 1

              update titles

                 set price = price * power(1.05, @time_thru_the_loop)

 

       4> GOTO语句

          语法:

              GOTO label

              ...

              label:

          示例:

              begin transaction

                 insert tiny(c1) values(1)

                 if @@error != 0 goto error_handler

                 commit transaction

                 return

              error_handler:

                 rollback transaction

                 return

 

       5> RETURN语句

          语法:

              RETURN

          (1)用于无条件退出一个批处理、存储过程或触发器。

               示例:

                   if not exists(select 1 from inventory

                                  where item_num = @item_num)

                   begin

                      raiseerror 51345 ‘Not Found’

                      return

                   end

                   print ‘No error found’

                   return

          (2)用于存储过程中返回状态值。

               示例:

                   create procedure titles_for_a_pub

                          (@pub_name varchar(40) = null)

                   as

                   if @pub_name is null

                      return 15

                   if not exists(select 1 from publishers

                                  where pub_name = @pub_name)

                      return –101

                   select t.tile from publishers p, titles t

                    where p.pub_id = t.pub_id

                      and pub_name = @pub_name

                   return 0   

    2、使用PL/SQL标准控制结构:

       1> 定义语句块

          语法:

              BEGIN

                 Statements ;

              END ;

 

       2> IF ... THEN ... ELSE语句

          语法:

              IF boolean_expression THEN

                 { statement | statement_block } ;

              [ELSIF boolean_expression THEN      /*注意此处的写法—— ELSIF */

                 { statement | statement_block } ;]

              ...

              [ELSE

                 { statement | statement_block } ;]

              END IF ;

 

          示例:

              v_NumberSeats rooms.number_seats%TYPE;

              v_Comment VARCHAR2(35);

              BEGIN

                SELECT number_seats

                  INTO v_NumberSeats

                  FROM rooms

                 WHERE room_id = 99999;

                IF v_NumberSeats

                   v_Comment := 'Fairly small';

                ELSIF v_NumberSeats

                   v_Comment := 'A little bigger';

                ELSE

                   v_Comment := 'Lots of room';

                END IF;

              END;

 

       3> 循环语句:

         (1)简单循环语句:

              语法:

                  LOOP

                     { statement | statement_block } ;

                     [EXIT [WHEN condition] ;]

                  END LOOP ;

                  其中,语句EXIT [WHEN condition];等价于

                      IF condition THEN

                         EXIT ;

                      END IF ;

              示例1:

                  v_Counter BINARY_INTEGER := 1;

                  BEGIN

                    LOOP

                      -- Insert a row into temp_table with the current value of the

                      -- loop counter.

                      INSERT INTO temp_table

                           VALUES (v_Counter, 'Loop index');

                      v_Counter := v_Counter + 1;

                      -- Exit condition - when the loop counter > 50 we will

                      -- break out of the loop.

                      IF v_Counter > 50 THEN

                         EXIT;

                      END IF;

                    END LOOP;

                  END;

 

              示例2:

                  v_Counter BINARY_INTEGER := 1;

                  BEGIN

                    LOOP

                      -- Insert a row into temp_table with the current value of the

                      -- loop counter.

                      INSERT INTO temp_table

                           VALUES (v_Counter, 'Loop index');

                      v_Counter := v_Counter + 1;

                      -- Exit condition - when the loop counter > 50 we will

                      -- break out of the loop.

                      EXIT WHEN v_Counter > 50;

                    END LOOP;

                  END;

 

         (2)WHILE循环语句:

              语法:

                  WHILE condition LOOP

                     { statement | statement_block } ;

                  END LOOP ;

 

              示例1:

                  v_Counter BINARY_INTEGER := 1;

                  BEGIN

                    -- Test the loop counter before each loop iteration to

                    -- insure that it is still less than 50.

                    WHILE v_Counter

                      INSERT INTO temp_table

                           VALUES (v_Counter, 'Loop index');

                      v_Counter := v_Counter + 1;

                    END LOOP;

                  END;

 

              示例2:

                  v_Counter BINARY_INTEGER;

                  BEGIN

                    -- This condition will evaluate to NULL, since v_Counter

                    -- is initialized to NULL by default.

                    WHILE v_Counter

                      INSERT INTO temp_table

                           VALUES (v_Counter, 'Loop index');

                      v_Counter := v_Counter + 1;

                    END LOOP;

                  END;

 

         (3)数字式FOR循环语句:

              语法:

                  FOR loop_counter IN [REVERSE] low_bound..high_bound LOOP

                     { statement | statement_block } ;

                  END LOOP ;

                  这里,loop_counter是隐式声明的索引变量。

 

              示例1:

                  FOR循环的循环索引被隐式声明为BINARY_INTEGER。在循环前面没有

                  必要声明它,如果对它进行了声明,那么循环索引将屏蔽外层的声明,

                  如下所示

                  v_Counter  NUMBER := 7;

                  BEGIN

                    -- Inserts the value 7 into temp_table.

                    INSERT INTO temp_table (num_col)

                      VALUES (v_Counter);

                    -- This loop redeclares v_Counter as a BINARY_INTEGER, which

                    -- hides the NUMBER declaration of v_Counter.

                    FOR v_Counter IN 20..30 LOOP

                      -- Inside the loop, v_Counter ranges from 20 to 30.

                      INSERT INTO temp_table (num_col)

                        VALUES (v_Counter);

                    END LOOP;

                    -- Inserts another 7 into temp_table.

                    INSERT INTO temp_table (num_col)

                      VALUES (v_Counter);

                  END;

 

              示例2:

                  如果在FOR循环中有REVERSE关键字,那么循环索引将从最大值向最

                  小值进行循环。请注意语法是相同的——仍然首先书写的是最小值,

                  如下所示

                  BEGIN

                    FOR v_Counter IN REVERSE 10..50 LOOP

                          NULL;

                    END LOOP;

                  END;

 

              示例3:

                  FOR循环中的最大值和最小值没有必要必须是数字型文字,它们可以

                  是能够被转换为数字值的任何表达式,如下所示

                  v_LowValue      NUMBER := 10;

                  v_HighValue    NUMBER := 40;

                  BEGIN

                    FOR v_Counter IN REVERSE v_LowValue..v_HighValue LOOP

                      INSER INTO temp_table

                          VALUES (v_Counter, ‘Dynamically sqecified loop range’);

                    END LOOP;

                  END;

 

       4> GOTO语句

          语法:

              GOTO label;

              ...

              >

              ...

 

          示例:

              v_Counter  BINARY_INTEGER := 1;

              BEGIN

                LOOP

                  INSERT INTO temp_table

                    VALUES (v_Counter, 'Loop count');

                  v_Counter := v_Counter + 1;

                  IF v_Counter > 50 THEN

                    GOTO l_EndOfLoop;

                  END IF;

                END LOOP;

             

                >

                INSERT INTO temp_table (char_col)

                  VALUES ('Done!');

              END;

 

       5> EXIT语句

          语法:

              EXIT;

          参见上面的PL/SQL标准控制结构之循环语句说明部分。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
SQL Server使用CROSS APPLY与OUTER APPLY实现连接查询SQL Server使用CROSS APPLY与OUTER APPLY实现连接查询Aug 26, 2022 pm 02:07 PM

本篇文章给大家带来了关于SQL的相关知识,其中主要介绍了SQL Server使用CROSS APPLY与OUTER APPLY实现连接查询的方法,文中通过示例代码介绍的非常详细,下面一起来看一下,希望对大家有帮助。

SQL Server解析/操作Json格式字段数据的方法实例SQL Server解析/操作Json格式字段数据的方法实例Aug 29, 2022 pm 12:00 PM

本篇文章给大家带来了关于SQL server的相关知识,其中主要介绍了SQL SERVER没有自带的解析json函数,需要自建一个函数(表值函数),下面介绍关于SQL Server解析/操作Json格式字段数据的相关资料,希望对大家有帮助。

聊聊优化sql中order By语句的方法聊聊优化sql中order By语句的方法Sep 27, 2022 pm 01:45 PM

如何优化sql中的orderBy语句?下面本篇文章给大家介绍一下优化sql中orderBy语句的方法,具有很好的参考价值,希望对大家有所帮助。

一文搞懂SQL中的开窗函数一文搞懂SQL中的开窗函数Sep 02, 2022 pm 04:55 PM

本篇文章给大家带来了关于SQL server的相关知识,开窗函数也叫分析函数有两类,一类是聚合开窗函数,一类是排序开窗函数,下面这篇文章主要给大家介绍了关于SQL中开窗函数的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下。

SqlServer创建自动收缩事务日志任务的图文详解SqlServer创建自动收缩事务日志任务的图文详解Sep 09, 2022 pm 01:41 PM

本篇文章给大家带来了关于SQL server的相关知识,SQL Server数据库存在一个问题,如果你限制了它的日志文件的大小,那么当数据库日志达到这个大小的时候,数据库就会停止写入日志,下面这介绍了关于SqlServer创建自动收缩事务日志任务的相关资料,希望对大家有帮助。

linux运行sql文件命令是什么linux运行sql文件命令是什么Mar 02, 2023 am 10:30 AM

linux运行sql文件命令是“psql -f test.sql”,其Linux运行sql脚本的方法是:1、使用shell工具登录到安装postgresql的服务器;2、编辑sql脚本内容;3、通过“psql -f test.sql”命令执行“test.sql”脚本即可。

SQL Server跨服务器操作数据库的图文方法(LinkedServer)SQL Server跨服务器操作数据库的图文方法(LinkedServer)Nov 02, 2022 pm 04:13 PM

本篇文章给大家带来了关于SQL的相关知识,其中主要介绍了SQL Server跨服务器操作数据库的图文方法,SQL Server Management Studio (SSMS) 是用于管理SQL Server 基础结构的集成环境,下面一起来看一下,希望对大家有帮助。

学习canvas框架 详解常用的canvas框架学习canvas框架 详解常用的canvas框架Jan 17, 2024 am 11:03 AM

探索Canvas框架:了解常用的Canvas框架有哪些,需要具体代码示例引言:Canvas是HTML5中提供的一个绘图API,通过它我们可以实现丰富的图形和动画效果。为了提高绘图的效率和便捷性,许多开发者开发了不同的Canvas框架。本文将介绍一些常用的Canvas框架,并提供具体代码示例,以帮助读者更深入地了解这些框架的使用方法。一、EaselJS框架Ea

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.