您好,欢迎访问一九零五行业门户网

常用SQL/oracle循环语句

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标准控制结构之循环语句说明部分。
其它类似信息

推荐信息