PL语句块中各种循环

来源:互联网 发布:xbox one 移动网络 dns 编辑:程序博客网 时间:2024/05/16 15:46

PL语句块中可以执行两大类语句:

1.SQL语言中的DML语言(数据操纵语言)和TCL(事务处理语言)。

2.过程语言,如循环、判断等语句。



1.DML语言

DML语言主要是insert、update、delete,因此可以在PL语句块中实现对数据的增添、更新、删除。


-----创建表:SQL> create table Bag(color varchar2(12),weight number);
Table created

----给表添加内容
declare
      color varchar2(12);
      weight number;
begin 
      color:='&颜色';
      weight:=&重量;
      insert into Bag values(color,weight);
end;
 ------删除表中内容
(declare)//可以省略
begin
    delete Bag where color='绿色'; 
end;
-------更新表中内容
(declare)
begin 
    update Bag set  weight=3 where color='绿色'; 
end;
    
-------系统报错。select不属于DML语言,属于DQL语言。
begin 
     select weight from Bag where color='黄色';
end;
       

2.TCL语言(待续)


 3.   -----循环语句-------

 ---------------------------------------------------

---1.for....loop循环 。

格式:for 变量名 in 条件(..)loop  [此后无分号]



declare
    i number;
begin 
    for i in reverse 1..10 loop
    dbms_output.put_line(i);
    end loop;
end;


---2.while....loop循环。

格式:while 条件 loop    



declare
    i number:=1;
begin
    while i<=10 loop
    dbms_output.put_line(i);
    i:=i+1;
    end loop;
end;


---3.loop exit when循环。

格式:(名称)loop (nei容)exit(名称)when 条件



declare
     i number:=-2;
begin
  loop exit when i>10;
  dbms_output.put_line(i);
  i:=i+1;
  end loop;
end;
---带循环名称
declare
     i number:=-2;
begin
  <<myloop>>//英文书名号内放名称
  loop exit myloop when i>10;
  dbms_output.put_line(i);
  i:=i+1;
  end loop;
  dbms_output.put_line('循环已结束');
end;


---4.loop if..then exit循环。

格式;(名称)loop (内容) if 条件 then (内容) exit


declare
      i number:=-2;
begin
  loop 
  i:=i+1;
  dbms_output.put_line(i);
  if i>10 then exit;
  end if;
  end loop;
end;


-----带名称
------------------------------------
declare
      i number:=-2;

begin

  <<myloop>>

  loop 
  i:=i+1;
  dbms_output.put_line(i);
  if i>10 then
  dbms_output.put_line('条件已满足');  
  exit myloop ;
  end if;
  end loop;
   dbms_output.put_line('if.. then..exit循环 ');
end;
1 0
原创粉丝点击