八、Oracle 之PlSql编程

1、PlSql编程 procedure language 过程语言

在数据库中的sql语句中,加入处理过程的语句 常见的定义变量 条件判断 循环结构
1).基本语法结构
declare
–声明部分
–类似定义变量的部分
begin
–处理逻辑代码
end;
2).属性声明方式
%type :声明变量和表中字段类型相同
%rowtype : 声明变量和表中一行数据类型一致

1、PlSql编程 procedure language 过程语言 在数据库中的sql语句中
    加入处理过程的语句  常见的定义变量 条件判断 循环结构
  1.基本语法结构
    declare
       --声明部分
       --类似定义变量的部分
    begin
       --处理逻辑代码
    end;
  2.属性声明方式  
    %type :声明变量和表中字段类型相同

    %rowtype : 声明变量和表中一行数据类型一致
*/

declare
    v1 number(2) :=1; --1)声明变量并赋值
    v2 number(10);    --2)声明变量

    v3  emp.ename%type ; --3)声明变量,类型为表中字段类型

    v4_row empCrowtype;  --4)存储表中一条记录


begin

    dbms_output.put_line('v1=='||v1);  --1)打印v1语句
    select sal into v2 from emp where empno = 7369; --2)查询并赋值 
    dbms_output.put_line('v2=='||v2); --打印v2语句

    select ename into v3 from emp where empno =7369; --3)查询并赋值
    dbms_output.put_line('v3=='||v3); 


    select * into v4_row  from emp where empno = 7369;    --4)查询一条记录并赋值
    dbms_output.put_line('v4_row---ename == '||v4_row.ename||'----eno=='||v4_row.empno);



end;

select * from emp

2、oracle的条件判断

if 条件 then
处理语句
elsif 条件2 then
处理语句
else
默认处理
end if; –if表达式结束

/*
  2、oracle的条件判断
  if  条件 then
     处理语句
  elsif 条件2 then   
     处理语句
  else
     默认处理
  end if;  --if表达式结束
*/

--需求:查询7499员工的工资 判断大小 输出汉字提示


declare 
sals number(6,2);

begin
  select sal into sals from emp where empno = 7499; 

  if sals>3000 then                        
    dbms_output.put_line('工资大于3000---'||sals);
  elsif sals < 1000 then
    dbms_output.put_line('工资小于1000---'||sals);
  else
      dbms_output.put_line('工资在1000到3000---'||sals); 

  end if;
end;

3、oracle的循环结构

a———————
loop
exit when 条件
end loop;
b———————
while 条件 loop
end loop;
c———————
for 变量 in 范围 loop
end loop;
.————————

/*
  3、oracle的循环结构
---------------------
     loop
       exit when 条件
     end loop;
---------------------
     while 条件 loop

     end loop;
---------------------
     for 变量 in 范围 loop 

     end loop;
------------------------
*/
--使用循环结构输出1---10的数字

--1)loop  exit when 条件  end loop;

declare
 v1 number(2) := 1;
begin

--开始循环 
loop  
  dbms_output.put_line(v1);

exit when v1 >= 10;
v1 := v1+1;

end loop;
--结束循环

end;
2) while 条件 loop  end loop;


declare

v1 number(2) :=1;
begin

while v1<=10 loop
  dbms_output.put_line(v1);
  v1 := v1+1;
  end loop;


end;


3) for 变量 in 范围 loop end loop 

declare

begin

for v1 in 1..10 loop
  dbms_output.put_line(v1);
  end loop;

end;

4、游标 相当于集合

1).概念
数据库使用游标实现 光标 用于接收查询得到的记录结果集合

2).游标使用步骤
①声明游标: cursor 游标名 is select 语句
②打开游标: open 游标名
③提取游标: fetch 游标名 into 记录变量
④关闭游标: close 游标名
3).游标的属性
%found
%notfound


/*
  4、游标 相当于集合
  1).概念
  数据库使用游标实现 光标 用于接收查询得到的记录结果集合

  2).游标使用步骤
  ①声明游标:  cursor 游标名 is select 语句
  ②打开游标:  open  游标名
  ③提取游标:  fetch 游标名 into 记录变量
  ④关闭游标:  close 游标名
  3).游标的属性
     %found
     %notfound

*/

--loop exit when ... end loop;

declare

cursor myCursor is select * from emp;  --1)声明游标并赋值
rowdata emp%rowtype;
begin

   open myCursor; --2打开游标

   loop 
     fetch myCursor into rowdata;  --3提取行信息
     exit when myCursor%notfound;   --4没有信息终止提取
     dbms_output.put_line('编号--'||rowdata.empno||'---姓名--'||rowdata.ename);

   end loop;  


   close myCursor;  --5关闭游标     


end;

--while ... loop  end loop  游标默认在表头,没有数据


declare 
cursor mycursor is select * from emp;  --1)声明游标并赋值
rowdata emp%rowtype;

begin

  --open mycursor; --开启
     fetch mycursor into rowdata; --提取 
       loop  fetch mycursor into rowdata; --提取 
       dbms_output.put_line('编号--'||rowdata.empno||'---姓名--'||rowdata.ename);
    end loop;

    close mycursor;

end;

--for .. in a...b loop end loop;

declare 
cursor mycursor1 is select * from emp;  --1)声明游标并赋值

begin

  open mycursor1; --开启
       for rowdata in mycursor1

       loop
         dbms_output.put_line('编号--'||rowdata.empno||'---姓名--'||rowdata.ename);  
       end loop;   

  close mycursor1; --关闭
end;

猜你喜欢

转载自blog.csdn.net/houysx/article/details/80142735