A
Anlannancy
Unregistered / Unconfirmed
GUEST, unregistred user!
我想建一个模块,去记录用户在线时长,
开发环境是delphi6.0+sql2000
要考虑到当用户死机或者掉线,但用户帐号仍然显示在线,这样就不能够准确记录用户的确切在线时长了。
我写的代码
CREATE PROCEDURE sp_userlog
@uname varchar(20),
@begin_date varchar(10),
@end_date varchar(10)
AS
set nocount on
delete userlog
DECLARE @id int,@date smalldatetime,@username varchar(20),@user_true_name varchar(20),@status varchar(20)
declare c1 cursor for
select * from user_log
where date>=@begin_date and date<=@end_date and username=@uname and status='在线'
open c1
Fetch next from c1 into @id,@date,@username,@user_true_name,@status
WHILE (@@FETCH_STATUS =0)
BEGIN
declare @id2 int,@date2 smalldatetime,@status2 varchar(20)
select @id2=(select top 1 [id] from user_log where id>@id)
select @date2=(select top 1 date from user_log where id>@id)
select @status2=(select top 1 status from user_log where id>@id)
if @status2='离线'
begin
insert into userlog([id],[date],username,user_true_name,status,date2,status2)
values( @id,@date,@username,@user_true_name,@status,@date2,@status2)
end
Fetch next from c1 into @id,@date,@username,@user_true_name,@status
END
update userlog set times=cast(DATEDIFF ( minute , date , date2 ) as varchar)+'分钟'
CLOSE c1
DEALLOCATE c1
GO
请各位大虾指点小弟,多谢~~~~~
开发环境是delphi6.0+sql2000
要考虑到当用户死机或者掉线,但用户帐号仍然显示在线,这样就不能够准确记录用户的确切在线时长了。
我写的代码
CREATE PROCEDURE sp_userlog
@uname varchar(20),
@begin_date varchar(10),
@end_date varchar(10)
AS
set nocount on
delete userlog
DECLARE @id int,@date smalldatetime,@username varchar(20),@user_true_name varchar(20),@status varchar(20)
declare c1 cursor for
select * from user_log
where date>=@begin_date and date<=@end_date and username=@uname and status='在线'
open c1
Fetch next from c1 into @id,@date,@username,@user_true_name,@status
WHILE (@@FETCH_STATUS =0)
BEGIN
declare @id2 int,@date2 smalldatetime,@status2 varchar(20)
select @id2=(select top 1 [id] from user_log where id>@id)
select @date2=(select top 1 date from user_log where id>@id)
select @status2=(select top 1 status from user_log where id>@id)
if @status2='离线'
begin
insert into userlog([id],[date],username,user_true_name,status,date2,status2)
values( @id,@date,@username,@user_true_name,@status,@date2,@status2)
end
Fetch next from c1 into @id,@date,@username,@user_true_name,@status
END
update userlog set times=cast(DATEDIFF ( minute , date , date2 ) as varchar)+'分钟'
CLOSE c1
DEALLOCATE c1
GO
请各位大虾指点小弟,多谢~~~~~