停用上次登录超过一个月的员工

时间:2018-03-29 03:10:19

标签: sql sql-server

我在下面发布了3个表格。我想创建一个程序,如果他/她的上一次登录超过一个月,并且如果他不是管理员,则会将员工的状态更新为'0'。

DROP TABLE EmployeeTest;
DROP TABLE SecurityTest;
DROP TABLE LOGINTest;

CREATE TABLE LOGINTest
(
    LoginID INT NOT NULL PRIMARY KEY,
    UserName Varchar(50),
    Password Varchar(50),
    LastLogin Date
);

INSERT INTO LOGINTest 
VALUES (1, 'Ovais', 'OV1234', '25-Mar-2018'),
       (2, 'Jason', 'JS1234', '17-Jun-2018'),
       (3, 'Michael', 'MC1234', '12-Mar-2018'),
       (4, 'Erock', 'EO1234', '28-Feb-2018');

CREATE TABLE SecurityTest
(
    SecurityID INT NOT NULL PRIMARY KEY,
    SecurityLevel Varchar(50)
);

INSERT INTO SecurityTest 
VALUES (0, 'Not Employee'), (1, 'Admin'), 
       (3, 'Manager'), (4, 'Clerk');

CREATE TABLE EmployeeTest
(
    EmployeeID INT NOT NULL PRIMARY KEY,
    EmployeeNAME Varchar(25),
    SecurityID INT,
    LoginID INT,
    Status INT,

    CONSTRAINT FK_Security 
        FOREIGN KEY (SecurityID) REFERENCES SecurityTest(SecurityID),
    CONSTRAINT FK_Login 
        FOREIGN KEY (LoginID) REFERENCES LoginTest(LoginID)
);

INSERT INTO EmployeeTest 
VALUES (1, 'Ovais Hanif', 3, 1, 1), 
       (2, 'Jason Stathom', 1, 2, 1),
       (3, 'Michael Melendiz', 4, 3, 1),
       (4, 'Erick Ortiz', 4, 4, 1);

我写的这个程序不正确,我知道。有人可以帮帮我吗?

问候。

这是我写的程序我不知道语法是否正确。

CREATE PROCEDURE LOGINPROC 
    SELECT 
        E.EMPLOYEEID, E.STATUS, L.LASTLOGIN, S.SECURITYLEVEL
    FROM 
        EMPLOYEETEST E
    INNER JOIN 
        SECURITYTEST S ON E.SECURITYID = S.SECURITYID
    INNER JOIN 
        LOGINTEST L ON E.LOGINID = L.LOGINID

BEGIN 
    UPDATE EMPLOYEETEST
    SET STATUS  = 0
    WHERE LASTLOGIN <= GETDATE-30 AND Lo
END;  

4 个答案:

答案 0 :(得分:2)

如果员工可以在LoginTest表中拥有多条记录,并且您希望获得最新的“LastLogin”值:

UPDATE e SET Status = 0
FROM  (SELECT * FROM EmployeeTest WHERE E.Status=1) e
INNER JOIN  (SELECT * FROM SecurityTest WHERE SecurityLevel<>'Admin') s on s.SecurityID=e.SecurityID
INNER JOIN  (SELECT LoginID, MAX(LastLogin) AS MostRecentLogin FROM LOGINTest GROUP BY LoginID) l on e.EmployeeID=l.LoginID
WHERE l.MostRecentLogin < DATEADD(MONTH,-1,cast(getdate() AS date))

答案 1 :(得分:1)

试试这个...

UPDATE  EmployeeTest
SET     Status                      =   0
FROM    EmployeeTest
JOIN    LOGINTest
  ON    (EmployeeTest.LoginID       =   LOGINTest.LoginID)
JOIN    SecurityTest
  ON    (EmployeeTest.SecurityID    =   SecurityTest.SecurityID)
WHERE   SecurityLevel               <>  'Admin'
  AND Status                        =   1
  AND LastLogin                     <   DATEADD(MONTH,-1,GETDATE())

答案 2 :(得分:0)

一个简单的连接应该可以做到这一点

示例

Update E set Status = 0
 From  EmployeeTest E
 Join  SecurityTest S on S.SecurityID=E.EmployeeID and S.SecurityLevel<>'Admin'
 Join  LOGINTest    L on E.EmployeeID=L.LoginID    and LastLogin < DateAdd(Month,-1,cast(GetDate() as date)) and E.Status=1

更新状态

EmployeeID  EmployeeNAME        SecurityID  LoginID Status
1           Ovais Hanif         3           1       1
2           Jason Stathom       1           2       1
3           Michael Melendiz    4           3       1
4           Erick Ortiz         4           4       0   --< Updated
  

编辑 - 可能是30天 - OP的选择

DateAdd(DAY,-30,cast(GetDate() as date))

答案 3 :(得分:0)

以下是更新员工状态的查询,该员工在过去一个月内未登录:

UPDATE EMPLOYEETEST
SET STATUS = 0
FROM EMPLOYEETEST E
INNER JOIN SECURITYTEST S ON E.SECURITYID = S.SECURITYID AND S.SecurityLevel <> 'Admin'
INNER JOIN LOGINTEST L ON E.LOGINID = L.LOGINID AND L.LastLogin < DATEADD(DAY,-30,CONVERT(DATE,GETDATE()))