有两个表
1,用户表users 包含字段 u_id(用户账号)、name(用户名)、active(是否有效,1为有效,0为无效)
2,记录用户登录信息表login 包含字段 l_id (用户账号)、l_time(记录登录时间)
想写一个存储过程,如果某一个用户id在60天内没有登录过系统,就将用户锁定(自动将users表的active字段设为0)
想用login表的l_time来限制,因为每次用户登录系统都会记录在login表,但是不知道这存储过程怎么写,求指点!多谢!
------解决方案--------------------
大概如下:(login_stat='Y'登录成功)
create or replace procedure usr_test(usr_id varchar2, login_stat out varchar2) is
l_usr_id varchar2(1000);
l_stat varchar2(5);
l_last_date date;
l_current_date date;
begin
select active into l_stat from users where u_id = usr_id;
if l_stat = 0 then
login_stat := 'N';
dbms_output.put_line('User status is inactive,can not login');
else
select max(l_time) into l_last_date from login where l_id = usr_id;
select sysdate into l_current_date from dual;
if l_current_date - l_last_date > 60 then
update users set active = 0 where u_id = usr_id;
login_stat := 'N';
dbms_output.put_line('User status is inactive,can not login');
else
login_stat := 'Y';
dbms_output.put_line('login successfully');
end if;
end if;
end usr_test;
------解决方案--------------------
初步想法,定期遍历user表的数据。
create or replace procedure XX
as
CURSOR cur_l is
select l_id,l_time from login ;
begin
for r_cur_l in cur_l loop
exit when cur_l%notfound or cur_l%notfound is null;
if sysdate -to_date( r_cur_l.l_time,'yyyymmdd')>60 then
update users set active=0 where u_id=r_cur_l.l_id;
end if;
end loop;
end;
如果可以直接获取l_id,也可以直接update了。
------解决方案--------------------
update users t set active=0
where active=1
and not exists(select 1 from login where i_id=t.u_id and l_time>sysdate-60);
commit;
需要用存储过程的话就加上begin end;