2011-11-25 50 views
1

我正在查找爲日誌表創建存儲過程的答案。以下是我需要編寫該SP的場景。如何爲日誌表創建存儲過程

無論何時用戶使用用戶唯一的LOGIN-ID登錄或註銷存儲登錄和退出時間。

此外,我必須限制用戶登錄,如果用戶以前的會話異常終止或用戶關閉應用程序沒有正確註銷,然後用戶需要等待10分鐘才能下一次登錄。

這是我的嘗試:

ALTER PROCEDURE USP_CHECKER(@LOGINID VARCHAR(70)) 
AS 
BEGIN 
    DECLARE @LASTID INT,@CURRENTTIME DATETIME,@TEMP INT,@HAS VARCHAR(30) 
    SELECT TOP 1 SLNO, @TEMP=(DATEDIFF(MI,LOGINDATETIME,GETDATE())) FROM TBL_LOGINDETAILS ORDER BY SlNo DESC 

    SET @HAS=(SELECT LoginID FROM TBL_LOGINDETAILS WHERE LogoutDateTime IS NULL) 
    IF @HAS IS NOT NULL 
    BEGIN 
    IF(@TEMP >= '10') 
     BEGIN 
     SET @CURRENTTIME = (SELECT DATEADD(MI, -10, GETDATE())) 
     UPDATE TBL_LOGINDETAILS SET [email protected] WHERE [email protected] 
     END 
    ELSE 
    RETURN @LASTID 
    --PREVENT FROM LOGIN 
    END 
    ELSE 
    INSERT INTO TBL_LOGINDETAILS (LoginID,LoginDateTime,isLogged) VALUES(@LOGINID,GETDATE(),'1') 
END 
+0

所以你已經**有**你的存儲過程 - 所以這是什麼問題,**完全**? –

+0

亞我有...但有一些性能問題。當我把它展示給她時,我的TL給人一種恐懼的樣子...... HHAH – joshua

回答

1

這是我嘗試過。

 USE [DB] 
GO 
/****** Object: StoredProcedure [dbo].[USP_SetLoginStatus] Script Date: 11/28/2011 10:58:47 ******/ 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
-- ============================================= 
ALTER PROCEDURE [dbo].[USP_SetLogStatus] 
    -- Add the parameters for the stored procedure here 
     @PLoginName  Varchar(75) 
AS 
BEGIN 
    DECLARE @SNO INT --VARIABLE TO HOLD THE SERAIL NUMBER 
    ,@ISLOGGED BIT  --HOLDING IS LOGGED STATUS 
    ,@TIMEDIFF VARCHAR(50) --VARIABLE TO HOLD TIME DIFFERENCE BETWEEN CURRENT LOGIN AND LAST LOG OUT TIME 

    -- SELECT QUERY FOR GETTING @TIMEDIFF AND SERIAL-NO VALUES FOR USER 

    SELECT @SNO=[SlNo],@ISLOGGED=[isLogged] ,@TIMEDIFF =DATEDIFF(MI, 
    (SELECT TOP (1) [LoginDateTime] FROM TBL_LOGINDETAILS WHERE [LogoutDateTime] IS NULL ORDER BY [SlNo]), 
    GETDATE()) FROM TBL_LOGINDETAILS WHERE [LoginID][email protected] 

    --IF USER IS LOGIN FIRST TIME 

    IF NOT EXISTS(SELECT isLogged FROM TBL_LOGINDETAILS WHERE [LoginID][email protected]) 
    INSERT INTO TBL_LOGINDETAILS(LoginID,isLogged,LoginDateTime) VALUES(@PLoginName,'1',GETDATE()) 
    ELSE 
    BEGIN 
     IF (@TIMEDIFF >=10) --LOGOUT TIME DIFF IS MORE THAN OR EQUALS TO 10 MINUTE 

    BEGIN 
     ----- UPDATING LOGOUT AND IS LOGGED FIELD 
     IF(@ISLOGGED =1) 
     BEGIN 
      UPDATE TBL_LOGINDETAILS SET [LOGOUTDATETIME]= (SELECT DATEADD(MI, -10, GETDATE())), [isLogged]=0 WHERE [LoginID][email protected] 
      AND [SlNo][email protected] 

     ----- INSERTING NEW LOG FOR CURRENT USER 

     INSERT INTO TBL_LOGINDETAILS([LoginID],[LoginDateTime],[isLogged]) VALUES(@PLoginName,GETDATE(),1) 
     END 
    ELSE 
     UPDATE TBL_LOGINDETAILS SET [LOGOUTDATETIME]= GETDATE() , [isLogged]=0 WHERE [LoginID][email protected] AND [SlNo][email protected] 
    END 
    ELSE 

    RETURN '0' 
    END 

END 
+0

你是否從你身邊發佈了答案?或者你需要一個建議? – eeerahul

2

一件事情跳出來的是你有一個變量,名爲INT@TEMP事實 - 但你要比較,就好像它是一個字符串:

IF(@TEMP >= '10') -- unnecessary single quotes! You're comparing against a string literal... 
BEGIN 
     SET @CURRENTTIME = (SELECT DATEADD(MI, -10, GETDATE())) 
     UPDATE TBL_LOGINDETAILS SET [email protected] WHERE [email protected] 
END 

這是一個INT - 所以你需要把它比對數值:

IF(@TEMP >= 10) -- **NO** unnecessary single quotes.... 
BEGIN 
    UPDATE dbo.TBL_LOGINDETAILS 
    SET LogoutDateTime = DATEADD(MI, -10, GETDATE()) 
    WHERE SLNO = @LASTID 
END 

而且 - 我肯定,你可以找到更適合自己,更有意義和更明顯的名稱,此變量比@TEMP ....