关于Oracle中select * from where 的存储过程

熟悉SQL Server的人,都会用这样的方法来创建存储过程:(查询student表的信息)
create  procedure  pro_sel_all
as
select * from  student

由于在Oracle中没有一个类型去接受一个结果集,而在SQL Server中却是由数据库系统自动返回结果集,然后显示。故如果我们用类似上面的方法来创建,如下:
create  or  replace  procedure  pro_sel_all
as
begin
select * from  student;
end  procedure  pro_sel_all;
则会报错:Warning: Procedure  created  with  compilation  errors.

那么在解决这个问题的时候有两个方法可以选择,第一种就是用游标来返回结果集合。
create  or  replace  procedure  pro_sel_all

is
 cursor  cur_sel_all  is  select  sno,sname,sage,ssёx from  student; --定义游标
     
c_sno student.sno%type;                                                    --声明变量分别保存student的各列
      c_sname student.sname%type;
      c_sage student.sage%type;
      c_ssex student.ssex%type;
begin
open cur_sel_all;
   loop                                                                --循环取数,并将游标数据填充到返回纪录集合中
     fetch cur_sel_all into c_sno,c_sname,c_sage,c_ssex;
     ёxit when cur_sel_all%NOTFOUND;
   --循环退出条件
     if cur_sel_all%FOUND then
                   --打印数据
       
dbms_output.put_line(c_sno||' '||c_sname||' '||c_sage||' '||c_ssex);   
     end if;
   end loop;
close cur_sel_all;
end;
/

exec pro_sel_all;                                        
--执行存储过程

我们也可以要用包(Package)来完成。在包的说明部分中声明游标和存储过程,在包体中给出存储过程。
则上面的存储过程,写为:
create  or  replace  package  pack_ sel_all        
------创建包的说明部分
as
type  mycursor  is  ref  cursor; --声明游标mycursor;
procedure  pro_sel_all (curp  out  mycursor );
--声明存储过程pro_sel_all;
end  pack_sel_all;
/

create  or  replace  package  body  pack_sel_all
  
------创建包体部分
is
procedure  pro_sel_all(curp  out  cursor)  --curp为游标类型的输出参数
begin

 
open  curp  for --将student表中的信息存放到curp中去
 select * from  student;

end  pro_sel_all;
end  pack_sel_all;
/
这个我花了一阵子才弄明白,弄的头都大了,没办法,脑子笨,呵呵,不过现在已经知道了。

 
熟悉SQL Server的人,都会用这样的方法来创建存储过程:(查询student表的信息)
create  procedure  pro_sel_all
as
select * from  student

由于在Oracle中没有一个类型去接受一个结果集,而在SQL Server中却是由数据库系统自动返回结果集,然后显示。故如果我们用类似上面的方法来创建,如下:
create  or  replace  procedure  pro_sel_all
as
begin
select * from  student;
end  procedure  pro_sel_all;
则会报错:Warning: Procedure  created  with  compilation  errors.

那么在解决这个问题的时候有两个方法可以选择,第一种就是用游标来返回结果集合。
create  or  replace  procedure  pro_sel_all

is
 cursor  cur_sel_all  is  select  sno,sname,sage,ssёx from  student; --定义游标
     
c_sno student.sno%type;                                                    --声明变量分别保存student的各列
      c_sname student.sname%type;
      c_sage student.sage%type;
      c_ssex student.ssex%type;
begin
open cur_sel_all;
   loop                                                                --循环取数,并将游标数据填充到返回纪录集合中
     fetch cur_sel_all into c_sno,c_sname,c_sage,c_ssex;
     ёxit when cur_sel_all%NOTFOUND;
   --循环退出条件
     if cur_sel_all%FOUND then
                   --打印数据
       
dbms_output.put_line(c_sno||' '||c_sname||' '||c_sage||' '||c_ssex);   
     end if;
   end loop;
close cur_sel_all;
end;
/

exec pro_sel_all;                                        
--执行存储过程

我们也可以要用包(Package)来完成。在包的说明部分中声明游标和存储过程,在包体中给出存储过程。
则上面的存储过程,写为:
create  or  replace  package  pack_ sel_all        
------创建包的说明部分
as
type  mycursor  is  ref  cursor; --声明游标mycursor;
procedure  pro_sel_all (curp  out  mycursor );
--声明存储过程pro_sel_all;
end  pack_sel_all;
/

create  or  replace  package  body  pack_sel_all
  
------创建包体部分
is
procedure  pro_sel_all(curp  out  cursor)  --curp为游标类型的输出参数
begin

 
open  curp  for --将student表中的信息存放到curp中去
 select * from  student;

end  pro_sel_all;
end  pack_sel_all;
/
这个我花了一阵子才弄明白,弄的头都大了,没办法,脑子笨,呵呵,不过现在已经知道了。

 

猜你喜欢

转载自blog.csdn.net/huiyeyeyey/article/details/82785303
今日推荐