我想从表T1复制数据到表T2到以下条件: 1)数据不应该重复。 2)和T1列签入和T2签入的时间差必须超过5秒。从一张表到另一张表插入没有重复记录
我现有的程序这
ALTER PROCEDURE [dbo].[usp_InsertUserAttendanceLog] @userId varchar(400),@CheckInCheckOutDate datetime,@WorkDate datetime,@InOutMode int,@VerifyMode int, @InsertDate datetime
AS
BEGIN
if
not exists (select 1 from tblAttendance where [email protected] and [email protected] and [email protected] and [email protected])
begin
if
not exists (select 1 from tblAttendance where [email protected] and convert(date,CheckInCheckOutDate)=convert(date,@CheckInCheckOutDate) and DATEDIFF(second,CheckInCheckOutDate,@CheckInCheckOutDate) <= 3)
begin
INSERT INTO tblAttendance(EmpCode,CheckInCheckOutDate,WorkDate,InoutMode,VerifyMode,InsertDate)
VALUES(@userId,@CheckInCheckOutDate,@WorkDate,@InOutMode,@VerifyMode,@InsertDate)
end
end
end
和我tryed这样的代码,但不起作用。葛亭Dupicate
INSERT INTO tblAttendance (EmpCode,CheckInCheckOutDate,WorkDate,InOutMode,VerifyMode,InsertDate,[Status])
SELECT T1.EmpCode,T1.CheckInCheckOutDate,T1.WorkDate,T1.InOutMode,T1.VerifyMode,T1.InsertDate,T1.[Status]
FROM testAttendanceBulk T1
WHERE NOT EXISTS(select 1 from tblAttendance T2 where T2.CheckInCheckOutDate=T1.CheckInCheckOutDate
and T2.WorkDate=T1.WorkDate and T2.InOutMode=T1.InOutMode
and T2.VerifyMode=T1.VerifyMode and T2.empCode=T1.EmpCode
and convert(date,T2.CheckInCheckOutDate)=convert(date,T1.CheckInCheckOutDate)
and DATEDIFF(second,T2.CheckInCheckOutDate,T1.CheckInCheckOutDate) <= 5)
你能定义“不行”吗?你得到重复吗?你没有得到什么?你是否收到错误信息? –
@ Sean Lange geting重复感谢 –
不要害怕给你的查询添加一些空白,这样他们更容易阅读。文本sql的墙很难处理。您的哪些查询正在返回重复项?都?第一个?第二个?请记住,我们无法看到您的屏幕或阅读您的想法。我们可以提供帮助,但您必须向我们提供信息。这里是一个开始的好地方。 http://spaghettidba.com/2015/04/24/how-to-post-at-sql-question-on-a-public-forum/ –