日期:2014-05-16  浏览次数:20488 次

ORACLE存储过程警惕(删除记录)

错误

create or replace procedure del_team
(
? userid in t_user.id%type,
? teamid out t_user.teamid%type

)
as
begin
? select t.teamid into teamid from t_user t where t.id=userid;
? delete from t_user t where t.id=userid;
? delete from t_gps t where t.userid=userid;
commit;
end;

删除某条记录,会删除表的全部数据。

问题是变量命名问题,如下解决(切记变量命名,不与系统字段或是变量以及其他关键字冲突):

-------------------------------------------------------------------

正确

create or replace procedure del_team
(
? v_userid in t_user.id%type,
? v_teamid out t_user.teamid%type

)
as
begin
? select t.teamid into v_teamid from t_user t where t.id=v_userid;
? delete from t_user t where t.id=v_userid;
? delete from t_gps t where t.userid=v_userid;
commit;
end;

?