oracle,pl/sql问题

来源:百度知道 编辑:UC知道 时间:2024/06/07 03:47:01
已经存在一个表session(用于统计登陆情况的).3列(1:用户名user_id, 2:session_id(唯一性,每次登陆的Id),3:dl_sj),写一个pl/sql过程,统计每个user_id的登录次数并保存到new table中。并设计一个触发器,确保session表中插入或者删除数据时 实现到new table中。。

create or replace procedure CNTSession() is
begin
insert into newtable(user_id, cnt)
select user_id, count(1)
from session group by user_id;
end ;

触发器:
create table foo(a number);

create trigger session_foo
after insert or update or delete
on session for each row
begin
if inserting then
insert into newtable(user_id, cnt)
select user_id, count(1)
from session where session = :user_id group by user_id;
elsif updating then
update newtable set cnt = (
select user_id, count(1)
from session where session = :user_id group by user_id)
where user_id = :user_id;
elsif deleting then
delete from newtable where user_id = :user_id ;
end if;
end;