Oracle PL/SQL编程基础

一、if then 语句
create or replace procedure IfThenProc is
begin
  -- Created on 2008-1-19 by ADMINISTRATOR
declare
  ----定义游标
  CURSOR my_cursor IS
  SELECT * from master;
  ----定义变量,存放游标所取的数据
  my_rec my_cursor%ROWTYPE;
begin
   ----打开游标
   OPEN my_cursor;
   loop
   ----将游标的数据取到my_rec变量
   FETCH my_cursor INTO my_rec;
   ----当游标无数据时,退出本循环
   EXIT WHEN my_cursor%NOTFOUND;
  
   IF my_rec.admin_id>0 THEN
      dbms_output.put_line('test');
   END IF;
  
   END loop;
   CLOSE my_cursor;
end;
end IfThenProc;
二、loop 语句
create or replace procedure LoopProc is
begin
  declare
  -- Local variables here
  x number;
begin
  -- Test statements here
  x:=0;
  loop
     x:=x+1;
    dbms_output.put_line(to_char(x));
    exit when x=20;
  end loop;
end;
end LoopProc;
三、while语句
create or replace procedure WhileProc is
begin
  -- Created on 2008-1-19 by ADMINISTRATOR
declare
  -- Local variables here
  x number;
begin
  -- Test statements here
  x:=1;
  while x<10
    loop
        dbms_output.put_line(to_char(x)||'还小于10');
        x:=x+1;
    end loop;
end;
end WhileProc;
四、for 语句
create or replace procedure ForProc is
begin
  -- Created on 2008-1-19 by ADMINISTRATOR
begin
  -- Test statements here
  for I IN REVERSE 1 .. 10
  loop
      dbms_output.put_line('in='||to_char(I));
  end loop;
end;
end ForProc;
五、游标
create or replace procedure CursorProc is
begin
  -- Created on 2008-1-19 by ADMINISTRATOR
declare
  -- Local variables here
  cursor c1 is
  select admin_name,admin_id from master where rownum <11;
  v_name varchar2(60);
  v_id number(7,2);
begin
  open c1;
  fetch c1 into v_name,v_id;
  while c1%found
  loop
   dbms_output.put_line(v_name||to_char(v_id));
   fetch c1 into v_name,v_id;
  end loop;
  close c1;
end;
end CursorProc;

你可能感兴趣的:(oracle,sql,编程)