oracle-扫盲贴:存储过程实现增删改查

原文引入:http://blog.csdn.net/yangzhawen/article/details/8617179

 

  

oracle-扫盲贴:存储过程实现增删改查

分类: oracle 5382人阅读 评论(0) 收藏 举报

为公司一个项目没有接触过oracle的程序员准备的一个oracle如何使用proc实现增删改查,简单示例:

 

create table t1
(
sid number not null primary key,
sname varchar2(10)
)
tablespace test;


declare
a number :=1;
begin
loop 
insert into t1 values(a,'snow');
a:=a+1;
exit when a=100;
end loop;
end;


----1.insert


create or replace procedure proc_insert
(
sid number,
sname varchar2
)
is 
begin
  insert into scott.t1(sid,sname) values(sid,sname);
   dbms_output.put_line(' 影响的行数:   '||sql%rowcount);
  commit;
end
;


set serveroutput on
exec proc_insert(101,'snow');

----2.update

create or replace procedure proc_update
(
isid in number ,
nsname in varchar2 
)
is 
begin
  update scott.t1 set sname=nsname where sid=isid;
If  SQL%Found  Then
    DBMS_OUTPUT.PUT_LINE('更新成功!');
Else
    DBMS_OUTPUT.PUT_LINE('更新失败!');
End  If;
  commit;
end
;


set serveroutput on
exec proc_update(101,'ocpyang');


----3.delete


create or replace procedure proc_delete
(
isid in number 
)
is 
begin
  delete scott.t1  where sid=isid;
If  SQL%Found  Then
    DBMS_OUTPUT.PUT_LINE('删除成功!');
Else
    DBMS_OUTPUT.PUT_LINE('删除失败!');
End  If;
  commit;
end
;


set serveroutput on
exec proc_delete(101);

--------------4.select

--4.1变量(select ....into):单行查询操作


create or replace procedure proc_select0
(isid in t1.sid%type )  --输入参数
as
osid t1.sid%type;  --变量 
osname  t1.sname%type;   --变量 
begin
select sid,sname into osid, osname from t1 where sid=isid;
dbms_output.put_line(' 编号为'||osid|| ' , 的职工姓名为  '||osname );
exception
when no_data_found then
dbms_output.put_line('没有符合条件的记录!');
when too_many_rows then
dbms_output.put_line('返回的行数太多!');
when others then
dbms_output.put_line('发生意外错误!');
end;


set serveroutput on
exec proc_select0 (101);


---4.2显示游标:返单行单列记录 

create or replace procedure proc_select1
(isid in t1.sid%type )  --输入参数
as
cursor a is select sname from t1 where sid=isid;
osname t1.sname%type;
begin
open a;
fetch a into osname; 
if a%found then
dbms_output.put_line( '职工姓名为:'||osname );  --游标结果集中只有一列
else
dbms_output.put_line('没有符合条件的记录!');
end if;
close a;
end;
        
set serveroutput on
exec proc_select1 (101);

--4.3显示游标:返回单行多列记录
create or replace procedure proc_select2
(isid in t1.sid%type )  --输入参数
as
cursor a is select * from t1 where sid=isid ;
osname t1%rowtype;
begin
open a;
fetch a into osname; 
if a%found then 
dbms_output.put_line( '职工的编号为:'||osname.sid||';'||'的职工姓名为  '||osname.sname );
else
dbms_output.put_line('没有符合条件的记录!');
end if;
close a;
end;
  
      
set serveroutput on
exec proc_select2 (101);

---4.4显示游标(loop循环):返回多行多列记录

/*

exit when语句一定要紧跟在fetch之后。必避免多余的数据处理。 
处理逻辑需要跟在exit when之后。这一点需要多加小心。 
循环结束后要记得关闭游标。

*/

--方法1:基于表的记录变量接收游标数据


create or replace procedure proc_select31
--(isid in t1.sid%type )  --输入参数
as
cursor a is select * from t1 ;
osname t1%rowtype;
begin
open a;
loop
fetch a into osname;
exit when a%notfound;
dbms_output.put_line( '职工的编号为:'||osname.sid||';'||'的职工姓名为  '||osname.sname );
end loop;
close a;
end;
  
        
set serveroutput on
exec proc_select31 ;


--方法2:基于游标的记录变量接收游标数据


create or replace procedure proc_select32
as
cursor a is select * from t1 ;
cur_record a%rowtype;
begin
open a;
loop
fetch a into cur_record; 
exit when a%notfound;
dbms_output.put_line( '职工的编号为:'||cur_record.sid||';'||'的职工姓名为  '||cur_record.sname );
end loop;
close a;
end;
  
    
set serveroutput on
exec proc_select32 ;


--方法3:基于集合变量的接收游标数据 


create or replace procedure proc_select33
as
cursor a is select * from t1 ;
type cur_table_type is table of a%rowtype index by binary_integer;
cur_table cur_table_type;
i int;
begin
open a;
loop
i:=a%rowcount+1;
fetch a into cur_table(i); 
exit when a%notfound;
dbms_output.put_line( '职工的编号为:'||cur_table(i).sid||';'||'的职工姓名为  '||cur_table(i).sname );
end loop;
close a;
end;
      
set serveroutput on
exec proc_select33 ;

---4.5显示游标(while....loop循环):返回多行多列记录


/*

游标打开后,必须执行一次fetch语句,游标的属性才会起作用。所以使用while 循环时,
就需要在循环之前进行一次fetch动作。 
而且数据处理动作必须放在循环体内的fetch方法之前。循环体内的fetch方法要放在最后。否则就会多处理一次。
while循环是游标里最复杂的一种.

*/

create or replace procedure proc_select4
--(isid in t1.sid%type )  --输入参数
as
cursor a is select * from t1 ;
osname t1%rowtype;
begin
open a;
fetch a into osname; 
while a%found loop  --循环之前做个fetch
dbms_output.put_line( '职工的编号为:'||osname.sid||';'||'的职工姓名为  '||osname.sname );
end loop;
close a;
end;
         
set serveroutput on
exec proc_select4 ;


---4.6显示游标(for循环)(适合多个记录):返回多行多列记录


游标使用for循环不用open、fetch、close关闭游标.


--方法1:典型for循环


create or replace procedure proc_select5
as
cursor a is select * from t1 ;
begin
for  res in a loop
dbms_output.put_line( '职工的编号为:'||res.sid||';'||'的职工姓名为  '||res.sname );
end loop;
end;

set serveroutput on
exec proc_select5 ;


--方法2:简单for循环


create or replace procedure proc_select6
as
begin
for  res in ( select * from t1 ) loop
dbms_output.put_line( '职工的编号为:'||res.sid||';'||'的职工姓名为  '||res.sname );
end loop;
end;


set serveroutput on
exec proc_select6 ;


----4.7 ref游标(loop循环)


/***

怎么使用  REF游标 ?
 ①声明REF 游标类型,确定REF 游标类型;
  ⑴强类型REF游标:指定retrun type,REF 游标变量的类型必须和return type一致。
   语法:Type   REF游标名   IS   Ref Cursor Return  结果集返回记录类型;
  ⑵弱类型REF游标:不指定return type,能和任何类型的CURSOR变量匹配,用于获取任何结果集。
   语法:Type   REF游标名   IS   Ref Cursor;
 ②声明Ref 游标类型变量;
  语法:变量名  已声明Ref 游标类型;
  
 ③打开REF游标,关联结果集 ;
  语法:Open   Ref 游标类型变量   For   查询语句返回结果集;
  
 ④获取记录,操作记录;
  语法:Fetch    REF游标名 InTo   临时记录类型变量或属性类型变量列表;
  
 ⑤关闭游标,完全释放资源;
  语法:Close   REF游标名;


能够使用ref弱类型REF游标就不要使用强类型REF游标

***/

--案例1:ref弱类型游标:loop循环


create or replace procedure proc_select8
(
choice in varchar2
)
as
TYPE cur IS REF CURSOR;  --声明游标类型为ref
a cur;     --声明变量为ref游标类型
osname t1%rowtype;
begin
if  choice='full' then
open a for select * from t1;
loop
fetch a into osname; 
exit when a%notfound;
dbms_output.put_line( '职工的编号为:'||osname.sid||';'||'的职工姓名为  '||osname.sname );
end loop;
elsif choice='top' then
open a for select * from t1 where rownum<10;
loop
fetch a into osname; 
exit when a%notfound;
dbms_output.put_line( '职工的编号为:'||osname.sid||';'||'的职工姓名为  '||osname.sname );
end loop;
else
  dbms_output.put_line('请输入正确值full或top!谢谢配合!');
return;
end if;
close a;
end;
  
      
set serveroutput on
exec proc_select8('full') ;
exec proc_select8('top') ;


--案例2:ref强类型游标:loop循环


create or replace procedure proc_select9
as
TYPE cur IS REF CURSOR RETURN t1%RowType;  --声明游标类型为ref
a cur;     --声明变量为ref游标类型
osname t1%rowtype;
begin
open a for select * from t1; 
loop
fetch a into osname;
exit when a%notfound;
dbms_output.put_line( '职工的编号为:'||osname.sid||';'||'的职工姓名为  '||osname.sname );
end loop;
close a;
end;


   
set serveroutput on
exec proc_select9 ;

转载于:https://www.cnblogs.com/meimao5211/p/3385665.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/408397.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

每日一学:如何转换png图片为jpg图片

点击上方“算法猿的成长“&#xff0c;关注公众号&#xff0c;选择加“星标“或“置顶”总第 128 篇文章&#xff0c;本文大约 800 字&#xff0c;阅读大约需要 3 分钟背景最近在工作中会遇到需要将 png 图片转换为 jpg 图片的需求&#xff0c;主要原因也是 png 图片占的空间太…

react学习(30)---uncaught at check call: argument fn is undefined

出现这个报错是当前的dva.js的一个接口没有成功调用

lightoj 1020 (博弈水题)

lightoj 1020 A Childhood Game 链接&#xff1a;http://lightoj.com/volume_showproblem.php?problem1020 题意&#xff1a;一堆石子有 m 个&#xff0c;Alice和Bob可以从中取1个或者2个&#xff0c;如果Alice先取&#xff0c;最后取的人败&#xff1b;如果Bob先取&#xff0…

react学习(31)----react父传子

import React from react import Son from ./son class Father extends React.Component {constructor(props) {super(props)}state {info: 父组件,}handleChange (e) > {this.setState({info: e.target.value,})}render() {return (<div><input typetext value{…

python 数据模型

点击上方“算法猿的成长“&#xff0c;关注公众号&#xff0c;选择加“星标“或“置顶”总第 129 篇文章&#xff0c;本文大约 4500 字&#xff0c;阅读大约需要 15 分钟最近开始阅读《流畅的python》&#xff0c;也会开始更新这本书的学习笔记第一篇的内容是第一章 python 数…

Java基础之写文件——使用多个视图缓冲区(PrimesToFile2)

控制台程序。本例将对应于每个素数的数据以三个连续数据项的形式写入&#xff1a; 1、以二进制值表示的字符串长度值&#xff08;最好是整型&#xff0c;但本例使用double类型&#xff09;&#xff1b; 2、素数值的字符串表示”Primennn“&#xff0c;其中数字的位数明显是变化…

react学习(32)----onref

// 父组件 import React from react import Son from ./son import { Button } from antdclass Father extends React.Component {child: anyconstructor(props) {super(props)}sonRef (ref) > {this.child ref // 在这里拿到子组件的实例}clearSonInput () > {this.c…

每日一学:如何用matplotlib展示图片

点击上方“算法猿的成长“&#xff0c;关注公众号&#xff0c;选择加“星标“或“置顶”总第 130 篇文章&#xff0c;本文大约 1000 字&#xff0c;阅读大约需要 5 分钟前言今天简单介绍如何通过 matplotlib 展示图片&#xff0c;分为以下几种情况&#xff1a;直接用 matplotli…

Servlet 过滤器

一、过滤器介绍 在Servlet 2.3中定义了过滤器&#xff0c;它能够对Servlet容器的请求和响应进行检查和修改。 Servlet过滤器能够在Servlet被调用之前检查Request对象&#xff0c;并修改Request Header 和 Request内容。 Filter可以过滤Servlet&#xff0c;JSP&#xff0c;HTML。…

每日一学:如何读取网络图片

点击上方“算法猿的成长“&#xff0c;关注公众号&#xff0c;选择加“星标“或“置顶”总第 131 篇文章&#xff0c;本文大约 1300 字&#xff0c;阅读大约需要 3 分钟前言有时候我们需要读取的图片是网络上的图片&#xff0c;应该怎么操作呢&#xff1f;这里也是介绍两个常用…

Windows Phone 如何振动手机?

1. 导入命名空间。 using Windows.Phone.Devices.Notification; 2. 通过调用对 VibrationDevice 类的静态 GetDefault 方法获取对振动控制器的引用。 VibrationDevice vibrationDevice VibrationDevice.GetDefault(); 3. 通过调用 VibrationDevice 类的 Vibrate 方法开始振动。…

编写高效的PyTorch代码技巧(上)

点击上方“算法猿的成长“&#xff0c;关注公众号&#xff0c;选择加“星标“或“置顶”总第 132 篇文章&#xff0c;本文大约 7000 字&#xff0c;阅读大约需要 20 分钟原文&#xff1a;https://github.com/vahidk/EffectivePyTorch作者&#xff1a;vahidk前言这是一份 PyTorc…

react学习(35)----getFieldDecorator will override value

我在自定义组件中定义了value值&#xff0c;getFieldDecorator会覆盖我们定义的值&#xff0c; 需要添加默认值可以使用在getFieldDecorator的时候&#xff0c;设置initialValue&#xff0c; 删除在自定义组件中定义的value就可以了&#xff01;

【ATT】Reverse Nodes in k-Group

ListNode* reverseBetween(ListNode* prev,ListNode* next)//reverse prev->next, last->prev之间的链表{ListNode* last prev->next;ListNode* cur last->next;while(cur!next){last->next cur->next;cur->next prev->next;prev->next cur;cu…

编写高效的PyTorch代码技巧(下)

点击上方“算法猿的成长“&#xff0c;关注公众号&#xff0c;选择加“星标“或“置顶”总第 133 篇文章&#xff0c;本文大约 3000 字&#xff0c;阅读大约需要 15 分钟原文&#xff1a;https://github.com/vahidk/EffectivePyTorch作者&#xff1a;vahidk前言这是一份 PyTorc…

统计(1 - 2)

统计学基础定义 Statistics的前部分为“state”&#xff0c;政府&#xff0c;原由是统计是300年前被首次应用在政府部门统计人口出生和死亡信息的&#xff1b;如今的统计学早已被应用在各个专业领域&#xff1b; 统计学是用以收集数据、分析数据和数据推论的一组概念、原则和方…

2020年计算机视觉学习指南

点击上方“算法猿的成长“&#xff0c;关注公众号&#xff0c;选择加“星标“或“置顶”总第 134 篇文章&#xff0c;本文大约 3000 字&#xff0c;阅读大约需要 10 分钟原文&#xff1a;https://towardsdatascience.com/guide-to-learn-computer-vision-in-2020-36f19d92c934作…

是选择Keras还是PyTorch开始你的深度学习之旅呢?

点击上方“算法猿的成长“&#xff0c;关注公众号&#xff0c;选择加“星标“或“置顶”总第 135 篇文章&#xff0c;本文大约 7000 字&#xff0c;阅读大约需要 20 分钟原文&#xff1a;https://medium.com/karan_jakhar/keras-vs-pytorch-dilemma-dc434e5b5ae0作者&#xff1…

关于myeclipse打开jsp巨慢解决方案

作为企业级开发最流行的工具&#xff0c;用Myeclipse开发java web程序无疑是最合适的&#xff0c;java web前端采用jsp来显示&#xff0c;myeclipse默认打开jsp的视图有卡顿的现象&#xff0c;那么如何更改jsp默认的打开方式&#xff0c;让我们可以进行更快速的jsp开发呢? 简单…