mysql和oracle详细区分

游标

游动的标识,是系统为用户开设的一个数据缓冲区,存放SQL的语句的执行结果。

隐式游标4个属性

SQL%ROWCOUNT 总行数
SQL%FOUND CRUD成功与否的true/false值
SQL%NOTFOUND 失败与否的true/false值
SQL%ISOPEN 显式游标打开与否的true/false值

显式游标

mysql:

set serveroutput

  1. declare声明:declare 游标名 cursor for 查询语句(结果集)
  2. open打开:open 游标名
  3. fetch取值:fetch 游标名 into 变量
  4. close关闭:close 游标名

oracle:

cursor 游标名 is 查询语句(结果集)

create procedure p6() # 创建存储过程语法 create procedure 存储过程名()
begin
declare row_cat_id int;
declare row_cat_name varchar(90);
declare row_parent_id int;
declare ergodic int default 1;
declare getcategory cursor for select cat_id,cat_name,parent_id from category; // 声明定义游标
declare exit handler for NOT FOUND set ergodic:=0; # 给游标定义越界标示。声明处理的hanlder不再是continue,而是exit即可达到多余读取最后一条数据的目的
open getcategory; # 打开游标
repeat # repeat … until 某变量条件 end repeat;
fetch getcategory into row_cat_id,row_cat_name,row_parent_id;
select row_cat_id,row_cat_name,row_parent_id;
until ergodic=0 end repeat; # 循环结束条件
close getcategory; # 关闭游标
end$
call p6()$ # 调用存储函数

oracle分页

查询薪水在top10中,排名1-5名的员工名,员工工作,员工薪水
使用rownum进行排序,使用between and进行分页

select ename,job,sal,no from
	(select t.*,rownum as no from
		(select *  from emp order by sal desc)t where rownum<=10)
where no between 1 and 5;

mysql分页

查询学生年龄最小的5名学生所有信息
mysql对查询出的结果使用limit进行分页

select * from
	(select * from student order by sage asc) AS t
limit 0,5

猜你喜欢

转载自blog.csdn.net/m0_47119598/article/details/112980543