我要执行的操作是,如果用户A的时钟时间为2019/04/29,那么用户A将无法在该日期期间时钟输入,直到明天或第二天。
因此,该SQL语句基本上可以正常工作,但不会向用户A显示任何错误,并且将插入重复记录。
IF EXISTS(select createddate from clockin where username = @Username and eventname = @b
group by createddate
HAVING COUNT(createddate) = 2
)
这也不起作用,它不允许插入任何其他日期,因为存在29/04,因此不允许输入30/04。
HAVING COUNT(createddate) = 1
如果用户尝试在2019/04/29上两次登录并允许他们在其他任何日期上登录,我希望显示错误。
逻辑
if (user and createddate doesn't exist)
{
allow clock in
}
else if user and createddate exists)
{
deny clock in, show an error message.
}
else if (user exists but createddate doesn't match any of the dates) -- to allow user to clock in on other days
{
allow clock in
}
我对如何做到这一点有些困惑。
答案 0 :(得分:1)
检查以下脚本。这应该达到您的目的-
IF NOT EXISTS(
SELECT * FROM clockin
WHERE CONVERT(VARCHAR, createddate, 101) = CONVERT(VARCHAR, GETDATE(), 101)
AND username = @Username
AND eventname = @b
)
BEGIN
--You can perform your Clocked steps here
PRINT 'Not Clocked Today'
END
ELSE
PRINT 'Already Clocked Today'