0

我想從表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) 

it's my table structure

+2

你能定義「不行」嗎?你得到重複嗎?你沒有得到什麼?你是否收到錯誤信息? –

+0

@ Sean Lange geting重複感謝 –

+1

不要害怕給你的查詢添加一些空白,這樣他們更容易閱讀。文本sql的牆很難處理。您的哪些查詢正在返回重複項?都?第一個?第二個?請記住,我們無法看到您的屏幕或閱讀您的想法。我們可以提供幫助,但您必須向我們提供信息。這裏是一個開始的好地方。 http://spaghettidba.com/2015/04/24/how-to-post-at-sql-question-on-a-public-forum/ –

回答

1

很確定你可以使用類似這樣的東西。請注意我對DATEFIFF謂詞的評論,它可能是您面臨的問題的一部分。

INSERT INTO tblAttendance 
(
    EmpCode 
    , CheckInCheckOutDate 
    , WorkDate 
    , InOutMode 
    , VerifyMode 
    , InsertDate 
    , [Status] 
) 
SELECT ab.EmpCode 
    , ab.CheckInCheckOutDate 
    , ab.WorkDate 
    , ab.InOutMode 
    , ab.VerifyMode 
    , ab.InsertDate 
    , ab.[Status] 
FROM testAttendanceBulk ab 
LEFT JOIN tblAttendance a ON a.CheckInCheckOutDate = ab.CheckInCheckOutDate 
    AND a.WorkDate = ab.WorkDate 
    AND a.InOutMode = ab.InOutMode 
    AND isnull(a.VerifyMode, 0) = isnull(ab.VerifyMode, 0) 
    AND a.empCode = ab.EmpCode 
    AND CONVERT(DATE, a.CheckInCheckOutDate) = CONVERT(DATE, ab.CheckInCheckOutDate) 
    AND DATEDIFF(SECOND, a.CheckInCheckOutDate, ab.CheckInCheckOutDate) <= 5 --do you really want 5 here? Your original query had 3. 
WHERE a.empCode IS NULL 
+0

5或3它不是問題謝謝 –

+0

好吧。你試過這個查詢嗎? –

+0

無法正常工作\t 如果我執行消息(100行受影響)如果我再次執行再次顯示(100行受影響)它已經厭倦了我。謝謝 –

0

在你加入,你正在檢查是否CheckinCheckoutDate是相等的,如果有> 5秒的差異。嘗試擺脫t1.CheckinCheckoutDate = T2.CheckinCheckoutDate;

INSERT 
     INTO tblAttendance 
      (EmpCode, 
      CheckInCheckOutDate, 
      WorkDate, 
      InOutMode, 
      VerifyMode, 
      InsertDate, 
      [Status]) 
    SELECT 
    DISTINCT T1.EmpCode, 
      T1.CheckInCheckOutDate, 
      T1.WorkDate, 
      T1.InOutMode, 
      T1.VerifyMode, 
      T1.InsertDate, 
      T1.[Status] 
     FROM testAttendanceBulk T1 
    WHERE NOT EXISTS(SELECT T1.EmpCode, 
          T1.CheckInCheckOutDate, 
          T1.WorkDate, 
          T1.InOutMode, 
          T1.VerifyMode, 
          T1.InsertDate, 
          T1.[Status] 
         FROM tblAttendance T2 
         WHERE 1=1 --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) 
+0

如果我執行消息(100行受影響)如果我再次執行再次顯示(100行受影響),它是厭倦了我。謝謝 –

+0

好的,所以從你的評論我明白,你不想插入重複,在這種情況下,你可以加入你插入的表格。讓我嘗試更新代碼。 –

+0

更新了我的代碼,您正在執行T2.CheckInCheckOutDate = T1.CheckInCheckOutDate AND AND DATEDIFF(second,T2.CheckInCheckOutDate,T1.CheckInCheckOutDate)<= 5)。如果大於等於0,則它們不能相等。 –

相關問題