2015-09-23 72 views
0

我試圖從舊錶中插入記錄到同一張表的新版本。問題在於,舊錶是由一位自學數據庫人員在16年前製作的,他明顯跳過了外鍵(因此每個表都有一個自動編號主鍵並且沒有完整性約束)。我希望把所有的記錄,並將其插入到我的表,該表具有以下關鍵:SQL Server 2008從計數查詢中插入不重複

(ProjectID, SubProjectID, PhaseID, SubPhaseID, DisciplineID) 

我做了一個快速的查詢到組上,並指望在舊錶上面的鍵,我得到480基本上記錄了我的新表中的非唯一鍵。

我的問題:是否有可能做出一個查詢,使我的計數查詢中沒有出現的所有行都插入到新表中?因爲我的計數查詢分組在5個字段上,我希望插入17個字段的完整記錄。

我的嘗試到目前爲止返回0條記錄,所以我希望接近。 此外,我無法在此找到任何已經在SO所以道歉,如果這是一個重複

INSERT INTO [ERP].[dbo].[Fees] 
      ([ProjectID] 
      ,[SubProjectID] 
      ,[PhaseID] 
      ,[SubPhaseID] 
      ,[DisciplineID] 
      ,[DatePhaseCommenced] 
      ,[TotalFee] 
      ,[EnggFee] 
      ,[DraftFee] 
      ,[ProposalDate] 
      ,[Comment] 
      ,[AuthDate] 
      ,[AuthDescription] 
      ,[HourlyRate] 
      ,[Closed] 
      ,[ClosedDate] 
      ,[SubmissionDate]) 
    SELECT 
     F.ProjectNo, 1, F.PhaseID, F.SubPhase, F.DisciplineID, '1/1/2000', 
     F.Amount, F.Engineering, F.Drafting, F.[Proposal Date], 
     F.Comment, F.AuthDate, F.AuthDescription, F.HourlyRate, 
     CASE 
      WHEN F.DontBookTime = 1 THEN 1 
      WHEN F.DontBookTime_Date IS NOT NULL THEN 1 
      ELSE 0 
     END, 
     F.DontBookTime_Date, F.SubmissionDate 
    FROM 
     Multitech.dbo.Fees F 
    WHERE 
     NOT EXISTS (SELECT 
         F.ProjectNo, F.PhaseID, F.SubPhase, F.DisciplineID 
        FROM Multitech.dbo.Fees F 
        WHERE (F.Amount > 0 OR F.Engineering > 0 OR F.Drafting > 0) 
        GROUP BY 
         F.ProjectNo, F.PhaseID, F.SubPhase, F.DisciplineID 
         HAVING (COUNT(*) > 1)) 
GO 

更新::

我意識到我可以只是其中不存在主鍵執行插入我的查詢,然後用適當的值從原始更新。該查詢也有一些小問題。

選擇重複查詢:

SELECT F.ProjectNo, F.PhaseID, F.SubPhase, F.DisciplineID 
FROM Multitech.dbo.Fees F 
GROUP BY F.ProjectNo, F.PhaseID, F.SubPhase, F.DisciplineID 
HAVING (COUNT(*) > 1) 

然後在重複的查詢中選擇所有未:

SELECT F.ProjectNo, F.PhaseID, F.SubPhase, F.DisciplineID 
FROM Multitech.dbo.Fees F 
WHERE NOT EXISTS (SELECT 
         F.ProjectNo, F.PhaseID, F.SubPhase, F.DisciplineID 
        FROM Multitech.dbo.Fees F 
        GROUP BY 
         F.ProjectNo, F.PhaseID, F.SubPhase, F.DisciplineID 
        HAVING (COUNT(*) > 1)) 

但仍沒有返回值...

+1

JOIN表'WHEN F.DontBookTime_Date <> NULL'將永遠** **是假的 - 你不能使用普通的等式和不等式運營商'NULL' - 你只能** **檢查'IS NULL'或'IS NOT NULL'對潛在的'NULL'值... –

+0

啊是的,我一直在SQL中犯這個錯誤。我解決了這個問題,但它仍然導致0行使用該大插入查詢 –

回答

1

首先,你在WHERE條款 你的條件犯了一個錯誤

WHERE NOT EXISTS (SELECT ...) 

只能是true或false(取決於返回任何記錄您的SELECT ...或沒有),但你試圖篩選與布爾條款你的記錄。

二,WHERE NOT EXISTS .. HAVING (COUNT(*) > 1)HAVING (COUNT(*) = 1)是一樣的,對嗎?

三,使用的而不是使用WHERE NOT EXISTS

SELECT 
      F.ProjectNo, 1, F.PhaseID, F.SubPhase, F.DisciplineID, '1/1/2000', 
      F.Amount, F.Engineering, F.Drafting, F.[Proposal Date], 
      F.Comment, F.AuthDate, F.AuthDescription, F.HourlyRate, 
      CASE 
       WHEN F.DontBookTime = 1 THEN 1 
       WHEN F.DontBookTime_Date IS NOT NULL THEN 1 
       ELSE 0 
      END, 
      F.DontBookTime_Date, F.SubmissionDate 
     FROM 
      Multitech.dbo.Fees F 
     JOIN 
      (SELECT F.ProjectNo, F.PhaseID, F.SubPhase, F.DisciplineID 
         FROM Multitech.dbo.Fees F 
         WHERE (F.Amount > 0 OR F.Engineering > 0 OR F.Drafting > 0) 
         GROUP BY 
          F.ProjectNo, F.PhaseID, F.SubPhase, F.DisciplineID 
          HAVING (COUNT(*) = 1)) as Count_F 
     ON F.ProjectNo = Count_F.ProjectNo and F.PhaseID = Count_F.PhaseID 
     and F.SubPhase = Count_F.SubPhase and F.DisciplineID = Count_F.DisciplineID 
0

你可以用它來交檢查:用-1代替整數

SELECT F.ProjectNo, F.PhaseID, F.SubPhase, F.DisciplineID 
FROM Multitech.dbo.Fees F 
WHERE NOT EXISTS (SELECT 1 FROM Multitech.dbo.Fees X 
where 
Isnull(F.ProjectNo,'') = Isnull(X.ProjectNo,'') and 
Isnull(F.PhaseID,'') = Isnull(X.PhaseID,'') and 
Isnull(F.SubPhase,'') = Isnull(X.SubPhase,'') and 
Isnull(F.DisciplineID ,'') = Isnull(X.DisciplineID ,'') 
GROUP BY 
X.ProjectNo, X.PhaseID, X.SubPhase, X.DisciplineID 
HAVING (COUNT(*) > 1))