oracle 存储过程实现增删改查
2013-03-02 16:57
330 查看
公司一个项目没有接触过oracle的程序员准备的一个oracle如何使用proc实现增删改查,简单示例:
create table t1
(
sid number not null primary key,
sname varchar2(10)
)
tablespace test;
----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(103,'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_update(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之后。这一点需要多加小心。
循环结束后要记得关闭游标。
*/
create or replace procedure proc_select3
--(isid in t1.sid%type ) --输入参数
as
cursor a is select * from t1 ;
osname t1%rowtype;
begin
open a;
fetch a into osname;
loop
dbms_output.put_line( '职工的编号为:'||osname.sid||';'||'的职工姓名为 '||osname.sname );
fetch a into osname;
exit when a%notfound;
end loop;
close a;
end;
set serveroutput on
exec proc_select3 ;
---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 );
fetch a into osname;
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 ;
create table t1
(
sid number not null primary key,
sname varchar2(10)
)
tablespace test;
----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(103,'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_update(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之后。这一点需要多加小心。
循环结束后要记得关闭游标。
*/
create or replace procedure proc_select3
--(isid in t1.sid%type ) --输入参数
as
cursor a is select * from t1 ;
osname t1%rowtype;
begin
open a;
fetch a into osname;
loop
dbms_output.put_line( '职工的编号为:'||osname.sid||';'||'的职工姓名为 '||osname.sname );
fetch a into osname;
exit when a%notfound;
end loop;
close a;
end;
set serveroutput on
exec proc_select3 ;
---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 );
fetch a into osname;
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 ;
相关文章推荐
- oracle-扫盲贴:存储过程实现增删改查
- oracle-扫盲贴:存储过程实现增删改查
- oracle-扫盲贴:存储过程实现增删改查
- java连接Oracle并实用存储过程实现增删改查(CRUD)
- .NET使用存储过程实现对数据库的增删改查
- Oracle实现创建作业定时执行存储过程
- C#连接oracle实现增删改查
- oracle-存储过程实现考生成绩查询-012
- oracle 使用存储过程实现分页(初学者,请各位大神指教)
- Oracle中通过:触发器,存储过程,Function调用实现解析Clob字段类型中存储的xml字符串
- Oracle用存储过程实现动态报表
- oracle-扫盲贴:存储过程实现增删改查
- 使用 Oracle的存储过程实现数据加密和解密
- Oracle 存储过程实现分页
- oracle实现存储过程的分页并用java代码调用存储过程
- 【VBA研究】利用ADO实现VBA连接Oracle并执行存储过程
- SQLSERVER自增字段和Oracle创建自增字段-ORACLE SEQUENCE方法的对比-并且用存储过程实现参数输出
- Oracle中通过Function,存储过程,触发器,调用实现解析Clob字段中存在的xml字符串
- 存储过程实现基本的增删查改(二)
- Oracle 存储过程实现行转列-pivot