pl*sql基础(一)

来源:互联网 发布:c语言循环程序代码 编辑:程序博客网 时间:2024/04/29 16:59
一、PLSQL简介
1、什么是PLSQL
Procedure Language SQL,过程化SQL语言。
它是Oracle数据库特有的编程语法,可以实现
一些复杂的运算及功能。
它是在SQL语言基础上加入了循环、判断等元素,
而提出的数据库编程方式。
PLSQL存储并运行在Oracle数据上。
2、PLSQL的作用
在数据库上直接运行,效率高。
适合于处理大数据量的运算。
3、优缺点
优点:执行效率高
缺点:非面向对象语言,编写及维护难度大。

开发效率低,测试效率低。

二、代码部分;

--plsql 入门
begin 
dbms_output.put_line('hello PLSQL');
end;
/
--声明变量
declare 
  str varchar2(20) :='我 like oracle';
  a number(10,0) :=5;
  b number(5) :=12323;
  c number(20,0);
begin 
 dbms_output.put_line('输出结果为:'||str);
 dbms_output.put_line('c='|| (a+b));
end;
/
--if  then--elsif then ... else end if;


declare 
  a number(10) :=123;
  b number(10):=431;
  c number(10) :=651;
begin 
 
  if a>b
  then 
   dbms_output.put_line('a>b '||' a减去b等于'||(a-b));
   elsif (a>c) 
   then 
    dbms_output.put_line('a大于c,差值为'||(a-c)); 
   else 
   dbms_output.put_line('a为最小值');
   end if;
  end;
  /
/*====================循环=======================*/
declare 
  i number(10) :=1;
  begin 
   loop --开始循环
    dbms_output.put_line(i);
    i:=i+1;
    exit when i>10;--退出循环的条件
    end loop;--结束循环
    end;
    /
 /*1-100累加的和*/   
declare 
 a number(10) :=1;
 b number(10) :=0;
 begin 
  loop
        b :=b+a;
         a :=a+1;
exit when a>100;
    end loop;
   dbms_output.put_line(b);
   end;
   /
/*================while循环==========*/
declare 
  a number(5) :=0;
begin 
  while (a<10) loop
  a:=a+1;
  dbms_output.put_line(a);
   end loop;
   end;
   /
   
/*====================for循环=========*/
begin 
  for i in 1..10 loop  ---定义一个数字集合
  dbms_output.put_line(i);
  end loop;
  end;
  /
  
  
/*======================对数据库的操作====================*/  


---创建表
 begin 
 
  execute immediate '
   create table test1 (
    id number(10) ,
    name varchar2(50)
   )
   ';
 end;
 ---增删改操作
 begin 
  update  test1 set name='123' where id=1;
 delete from test1 where id =2;
 insert into test1 values (101,'奥特曼');
  commit;
 end;
----查询将结果集封装到变量中去
declare 
  t_id varchar2(10);
  t_name varchar2(100);
begin 
 
  select id,name 
  into t_id,t_name from test1 where id=3;
  
  dbms_output.put_line('id='||t_id);
  dbms_output.put_line('name='||t_name);
 
end;


/*使用%type来为对应字段赋值*/
declare 
  test_id test1.id %type;
  test_name test1.name %type;
begin 
  select id ,name into test_id,test_name 
  from test1 where id =3;
  dbms_output.put_line('id='||test_id ||',name='||test_name);
 end; 
 /
 
 


/*=======================使用游标遍历多条记录===========================*/


declare 
 cursor test_cur  is 
  select * from test1;
  
  test_id test1.id%type;
  test_name test1.name %type;
  begin 
  
    open test_cur ;
     loop
     fetch test_cur into test_id ,test_name;
     exit when test_cur%notfound;
      dbms_output.put_line('id='||test_id ||',name='||test_name);
     end loop;
     close test_cur;
    
   end;
   /
   
 ---------使用while循环遍历游标
 declare 
  cursor t is select * from test1 ;
  t_id test1.id%type;
  t_name test1.name%type;
 begin 
    open t;
    --为何多出这一句话
     fetch t into t_id,t_name;
    while t%found 
   
    loop
     dbms_output.put_line('id='||t_id||',name='||t_name);
    fetch t into t_id,t_name;
   
    end loop;
    close t;
  end;  
  
  ---使用for循环遍历游标
  declare 
  cursor s is select * from test1;
 
  begin 
  --声明行变量,其结构与游标中的行数据一致,
--即具有test1表的结构。for循环会自动开启并关闭游标。
   for n in s loop
    dbms_output.put_line('id='||n.id ||',name='||n.name);
    end loop;
   end;
   
   /*==========%rowtype====*/
 declare 
    cursor r is select * from test1;
    --声明为行变量
    ts test1%rowtype;
begin 
   open r;
    fetch r into ts;
   while r%found 
   loop
   fetch r into ts;
  
   dbms_output.put_line(ts.id||ts.name);
   end loop;
   close r;
 end;
 /

  


 
 
    


    
  
  
  

0 0