自動把登錄的用戶信息記錄到一個表中

lula 2007-01-15 02:50:45
Oracle數據庫有很多個用戶﹐現在要實現有用戶登錄時自動把用戶名及登錄時間退出時間等信息寫了一個表中﹐表為logininfo(user_id,login_time,logout_time),怎么實現此功能﹖
...全文
235 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
adam0730 2007-01-19
  • 打赏
  • 举报
回复
好长,加点注释嘛,这是个好习惯
CH_MARS 2007-01-17
  • 打赏
  • 举报
回复
蹭分了
tgm78 2007-01-15
  • 打赏
  • 举报
回复
lz写个trigger即可

给你一个现成的例子,稍做改动即可使用

create or replace trigger tr_user_logon after logon on database
declare
v_username varchar2(30);
v_machine varchar2(64);
v_osuser varchar2(30);
v_logon_time date;
v_cnt number;
v_sess_cnt number;
v_proc_cnt number;
cursor cur_session is
select username, machine, osuser, logon_time
from v$session
where audsid = userenv('SESSIONID')
and username is not null
and username not in ('GBSMAN', 'GBSMGR', 'ACCTMAN', 'PDMAN', 'GBSPKG',
'GACPKG', 'GBSMIG', 'GBSTRAN', 'GBSDEBUG')
and username in
(select ltrim(rtrim(upper(user_name))) from gusermgr.gbs_user_tbl);
v_open_mode varchar2(20);
begin
if user<>'DGEXP' then

select count(*)
into v_cnt
from v$session
where audsid = userenv('SESSIONID')
and username is not null
and username not in ('SYS', 'SYSTEM', 'DBMGR');
if v_cnt > 0 then
select value_num
into v_proc_cnt
from gbs_parameter
where name = 'processes';
select count(*) into v_sess_cnt from v$session;
if v_sess_cnt >= v_proc_cnt then
raise_application_error(-20001,
'maximum number of processes (' ||
v_proc_cnt || ') exceeded');
end if;
end if;
open cur_session;
loop
fetch cur_session
into v_username, v_machine, v_osuser, v_logon_time;
exit when cur_session%NOTFOUND;
v_machine := rtrim(v_machine, chr(0));
insert into mon_logon_info
values
(v_username, v_machine, v_osuser, v_logon_time);
end loop;
close cur_session;
end if;
end;

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧