Click here to Skip to main content
15,896,153 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hi Everyone,

I am working on the below stored proc that counts total processes and display it through a label on the page.

SQL
USE [MyProc]
GO
/****** Object:  StoredProcedure [dbo].[AUDITINGHISTORY]    Script Date: 02/08/2011 09:40:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO



ALTER PROC [dbo].[AUDITINGHISTORY]-- 4
(
@userId BIGINT
)
AS
BEGIN
SET NOCOUNT ON

SELECT TOP 5 A.MobileNo AS 'Mobile No',A.Barcode AS 'Barcode',
CONVERT(NVARCHAR(50),A.CreatedOn,120) AS 'Audit Time'
FROM AuditFlagsView A WHERE A.CreatedBy=@userId ORDER BY A.CreatedOn DESC

DECLARE @Login NVARCHAR(50),@Logout NVARCHAR(50),@totalcnt BIGINT,@TempTime DATETIME

CREATE TABLE #Temp
(
rowId bigint identity,
Logintime DATETIME
)
INSERT INTO #Temp select TOP 2 LoginTime from UserLoginView WHERE Uid=@userId AND Logintype='Login' order by LoginTime DESC
select @Login=convert(nvarchar(50),Logintime,100) from #Temp where rowid=1
select @Logout=convert(nvarchar(50),Logintime,100) from #Temp where rowid=2

select @totalcnt=count(AuditId) from AuditFlagsView where CreatedBy=@userId AND
CONVERT(NVARCHAR(50),CreatedOn,103)=CONVERT(NVARCHAR(50),getdate(),103)

SELECT @Login as 'LogIn', @Logout as 'LogOut',@totalcnt as 'TotalCnt'
END

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

The procedure is working, but whenever I log on to a page, it shows Total Processed count as "1" instead of "0". I think I need to use the date function for this. Can anyone please suggest anything?
Posted
Updated 8-Feb-11 1:33am
v4
Comments
Raj.rcr 8-Feb-11 1:24am    
Is anybody thr? please reply
#realJSOP 8-Feb-11 8:01am    
You're going to have to better describe what you're trying to do, and why.
YangQy 8-Feb-11 15:37pm    
if procedure is working fine, it should be from the code. Why don't you post ur code?
Albin Abel 8-Feb-11 16:06pm    
Hi,

It is not clear how the audit entries get added to AuditFlagsView and what @ the view structure. How the tables are related in the view?. Please explain more.

Though it is need to use a better date comparison function it has to result is a 0 count if the conditions not satisfied. Is it sure there is no entry in the AuditFlagView which satisfy the condition at the time of login.

It looks like the problem not in this procedure, but may be on the view.

Also you are selecting the top 2 Logintime where the login and logout type is 'Login'. What if the user logged in, but didn't logged out or the app shutdown abruptly and again logged in. So there will be 2 login times, not login-logout time. Is that condition handled properly?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900