您的位置:首页 > 数据库 > Oracle

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 ;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: