oracle-扫盲贴:存储过程实现增删改查
原文引入:http://blog.csdn.net/yangzhawen/article/details/8617179
为公司一个项目没有接触过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 ;
oracle-扫盲贴:存储过程实现增删改查的更多相关文章
- Delphi - cxGrid连接Oracle数据库 实现数据的增删改查
cxGrid连接Oracle数据库 实现数据的增删改查 cxGrid连接Oracle数据库 1:通过OraSession连接数据库.OraDataSet实现OraSession和OraDataSour ...
- python链接oracle数据库以及数据库的增删改查实例
初次使用python链接oracle,所以想记录下我遇到的问题,便于向我这样初次尝试的朋友能够快速的配置好环境进入开发环节. 1.首先,python链接oracle数据库需要配置好环境. 我的相关环境 ...
- oracle初试、函数、增删改查、多表查询
安装oracle后的测试以及解锁账户 安装后打开命令行,输入 sqlplus 回车后会提示输入用户名,输入 sys或者system 回车后输入密码,密码为安装or ...
- oracle学习笔记系列------oracle 基本操作之表的增删改查
--创建一个表 CREATE TABLE employee_souvc( id ), name ), gender ), birth DATE, salary ,), job ), deptno ) ...
- C# WINFORM 窗体执行ORACLE存储过程 进行增删改查 自己编写借助网络(二)
窗体界面: 下面是项目二的代码 本代码我是留着备份学习的 以供参考: 存储过程: 存储过程: 插入数据:CREATE OR REPLACE Procedure p_insert_t_cls --存储过 ...
- SqlServer存储过程(增删改查)
* IDENT_CURRENT 返回为任何会话和任何作用域中的特定表最后生成的标识值. CREATE PROCEDURE [dbo].[PR_NewsAffiche_AddNewsEntity] ( ...
- 关于oracle数据库(5)增删改查
添加.修改.删除.查询都叫SQL语言(结构化查询语言) 添加数据(注意事项:列的顺序和值的顺序要相同.数量也要相同:字符串要加单引号,数字可以加或不加) insert into 表名(列名,列名,列名 ...
- Oracle表操作-创建及增删改查
数据类型: 1.CHAR:定长字符类型,默认长度是1,最长不超过2000字节. 2.CARCHAR2(length):可变字符类型,默认长度是1,最长不超过4000字符. 3.NUMBER(P,S): ...
- easyui datagrid 禁止选中行 EF的增删改查(转载) C# 获取用户IP地址(转载) MVC EF 执行SQL语句(转载) 在EF中执行SQL语句(转载) EF中使用SQL语句或存储过程 .net MVC使用Session验证用户登录 PowerDesigner 参照完整性约束(转载)
easyui datagrid 禁止选中行 没有找到可以直接禁止的属性,但是找到两个间接禁止的方式. 方式一: //onClickRow: function (rowIndex, rowData) ...
随机推荐
- BZOJ 3158 千钧一发 最小割
分析: 偶数对满足条件2,所有奇数对满足条件1. 如果你能一眼看出这个规律,这道题就完成了一半. 我们只需要将数分为两类,a值为奇数,就从S向这个点连容量为b值的边,a值为偶数,就从这个点向T连容量为 ...
- CSU1160
十进制-十六进制 Time Limit: 1 Sec Memory Limit: 128 MB Description 把十进制整数转换为十六进制,格式为0x开头,10~15由大写字母A~F表示. ...
- 优先队列重载运算符< 以及初始化列表
优先队列定义 priority_queue<int, vector<int>, greater<int> >pq; 优先队列重载<运算符 在结构体中定义一个 ...
- 全文搜索(A-3)-用户建模
用户模型可以分为静态模型.动态模型.混合推荐用户模型. 静态模型往往通过显式方式收集用户偏好信息: 动态模型通过隐式方式收集用户偏好信息: 基于内容的用户系统的推荐模型: 关键字匹配,空间向量模型 协 ...
- [luoguP1042] 乒乓球(模拟)
传送门 终于过了这sb题了. 当初我连这道题都A不了(╯▔皿▔)╯ 代码 #include <cstdio> #include <iostream> #define N 100 ...
- Django开发:(3.2)ORM:多表操作
表关系总结: 一对多:在多的表中建立关联字段 多对多:创建第三张表(关联表):id 和 两个关联字段 一对一:在两张表中的任意一张表中建立关联字段(关联字段一定要加 unique 约束) 子查询:一次 ...
- [K3Cloud] QueryService使用注意事项
QueryServlice是目前查询数据非常好用的服务,但目前在使用过程中由于使用不当产生不少问题,下面将一一解答: 1.在查询一些实体关键字段如实体主键.分录序号时,条件中的别名怎么会变来变去? ...
- 【BZOJ2527】Meteors(整体二分)
题意: Byteotian Interstellar Union有N个成员国.现在它发现了一颗新的星球,这颗星球的轨道被分为M份(第M份和第1份相邻),第i份上有第Ai个国家的太空站. 这个星球经常会 ...
- 「CodePlus 2017 12 月赛」火锅盛宴
n<=100000种食物,给每个食物煮熟时间,有q<=500000个操作:在某时刻插入某个食物:查询熟食中编号最小的并删除之:查询是否有编号为id的食物,如果有查询是否有编号为id的熟食, ...
- NOIP2012 文化之旅
题目描述 Description 有一位使者要游历各国,他每到一个国家,都能学到一种文化,但他不愿意学习任何一种文化超过一次(即如果他学习了某种文化,则他就不能到达其他有这种文化的国家).不同的国家可 ...