oracle 学习 (四)

/*
  序列: oracle 使用来模拟id自动增长的
*/
create sequence seq_test4;
create table test2(
  tid number primary key,
  tname varchar2(10)
);

insert into test2 values(seq_test4.nextval,'张三');
select *from test2;

/*
PLSQL编程:过程语言,编写一些复杂的业务逻辑
  输出星号: abs(y)+abs(x)<=m;
  
  msal emp.sal%type ---引用型变量
  mrow emp%rowtype  ---记录型变量
  
  select mrow into vsal from emp where empno=7839
*/
declare
 m number := 10; 
begin
  for y in -m..m loop
      for x in -m..m loop
        if abs(y)+abs(x)<=m then
          dbms_output.put('*');
        else
          dbms_output.put(' ');
        end if;
      end loop;
      dbms_output.new_line();
  end loop;
end;

/*
游标(光标):是用来操作查询结果集,相当于是JDBC中的ResultSet

   语法:cursor 游标名 is 查询结果集
   开发步骤:
      1.声明游标
      2.打开游标        open 游标名
      3.从游标中取数据  fetch 游标名 into  变量
                        游标名%found:找到数据
                        游标名%notfound:没有找到数据
      4.关闭游标        close 游标名 
   
   系统引用游标
       1.声明游标 :游标名 sys_refcursor
       2.打开游标: open 游标名 for 结果集
       3.从游标中取数据
       4.关闭游标
   for 循环变量游标:
      不需要声明额外变量
      不需要打开游标
      不需要关闭游标
*/
--输出员工表中所有员工姓名和工资(不带参数游标)
/*
  结果集:所有员工
  声明一个变量,用来记录一行数据 %rowtype
*/
declare
 --游标
 cursor vrows is select *from emp;
 --声明变量,记录一行数据
  vrow emp%rowtype;
begin 
  --1.打开游标
  open vrows;
  --2.从游标中提取数据
  --循环取数据
  loop
    fetch vrows into vrow;
    exit when vrows%notfound; 
    dbms_output.put_line('name:'||vrow.ename||',sal:'||vrow.sal);
  end loop;
  --3.关闭游标
  close vrows;
end;

--输出指定部门下的员工姓名和工资
/*
  游标:指定部门的所有员工
  声明一个变量记录一行数据
*/
declare
  --声明游标
  cursor vrows(dno number) is select *from emp where deptno=dno;
  --声明变量
  vrow emp%rowtype;
begin 
  --1.打开游标,指定10号部门
  open vrows(10);
  --2.循环变量,取数据
  loop
    fetch vrows into vrow;
    exit when vrows%notfound;
    dbms_output.put_line('name:'||vrow.ename||',sal:'||vrow.sal);
  end loop;
  --关闭游标
  close vrows;
  
end;

--系统引用游标
--输出员工表中所有员工姓名和工资
declare
  --声明系统引用游标
  vrows sys_refcurs
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值