2011-11-15 92 views
1

我想在我當前的SQL INSERT INTO語句中添加一個查詢。下面是我的表和當前的sql語句。C#SQL添加一個查詢檢查重複密鑰

我有3個表:

表1用戶名用戶名

表2用戶名狀態

表3用戶名用戶名問題

目前我只有一臺符合以上3個檢查3 SELECT語句並將結果插入到表3:

1. 
    Insert in to Table3(userid,issue) 
     SELECT t1.userid,'check no.1' 
     FROM table1 t1 
     FULL OUTER JOIN table2 t2 ON t1.userid = t2.userid 
     where t1.userid not null and t2.userid is null 
2. 
    Insert in to Table3(userid,issue) 
     SELECT t1.userid,'check no.2' 
     FROM table1 t1 
     inner JOIN table2 t2 ON t1.userid = t2.userid 
     where t2.status = 'DELETE' 

3. 
    Insert in to Table3(userid,issue) 
     SELECT t2.userid,'check no.3' 
     FROM table2 t2 
     right outer JOIN table2 t2 ON t1.userid = t2.userid 
     where t2.status <> 'DELETE' and t1.userid is null 

現在我想補充另一個檢查是否在T1和T2中檢查重複的用戶ID:

  1. 檢查具有相同大小寫的相同用戶ID(例如, E01和E01不應該存在)
  2. 檢查相同的用戶ID,但不同的上限(例如E01和E01不應該存在)

我如何可以編寫第四SQL查詢這對於重複的用戶ID專門檢查?

謝謝你的幫助。

+0

默認情況下,SQL查詢是不區分大小寫。 –

+1

@BrandonMoore:risky語句 - SQL Server中的大小寫敏感性取決於用於數據庫,表和/或列的**排序規則** ....它可以**區分大小寫或不區分大小寫。 –

+0

@Mark Care打賭他的排序規則是否區分大小寫?我爲了賺取一些快速現金而失望:) 但是,我知道你是對的。 –

回答

1

編輯:

INSERT INTO Table3(userid, Username, issue) 
SELECT 
    userid, Username, 'Duplicate UserID' 
FROM 
    (
     SELECT 
       userid, 
       Username, 
       RANK() OVER (PARTITION BY userid ORDER BY userid) AS [RANK] 
     FROM 
      Table1 --table2 
    ) X 
WHERE 
    [RANK]=2 


INSERT INTO Table3(userid, Username, issue) 
SELECT 
    userid, [status], 'Duplicate UserID' 
FROM 
    (
     SELECT 
       userid, 
       [status], 
       RANK() OVER (PARTITION BY userid ORDER BY userid) AS [RANK] 
     FROM 
      Table2 
    ) X 
WHERE 
    [RANK]=2 

您可以使用COLLATE如圖老款做了區分大小寫的檢查,如果你需要。

我用RANK = 2只插入一個重複的用戶ID

舊條目: 可以使用COLLATE做一個區分大小寫的檢查

INSERT INTO Table3(UserId, Username, Issue) 
    SELECT 
    COALESCE(t1.UserId, t2.UserId), 
    t1.UserName, 
    CASE   
     WHEN (t2.userid IS NULL AND t1.userid IS NOT NULL) 
      THEN 'User exists in t1 but not in t2' 
     WHEN (t2.status = 'DELETE' AND t1.userid IS NOT NULL) 
      THEN 'User Exists in t1, but status in t2 is DELETE' 
     WHEN (t2.userid IS NOT NULL AND t2.status != 'DELETE' AND t1.userid IS NULL) 
      THEN 'Non-Deleted user in t2 does not exist in t1' 
     WHEN t1.userid COLLATE SQL_Latin1_General_CP1_CS_AS = t2.userid COLLATE SQL_Latin1_General_CP1_CS_AS 
      THEN 'Duplicate userid with same caps' 
     WHEN t1.userid = t2.userid 
      THEN 'Duplicate userid but different caps' 
    END 
FROM table1 t1 
FULL OUTER JOIN table2 t2 ON t1.userid = t2.userid 
WHERE 
    (t1.userid IS NOT NULL AND t2.userid IS NOT NULL AND t2.status != 'DELETE' AND t1.userid = t2.userid) OR  
    (t2.userid IS NULL AND t1.userid IS NOT NULL) OR 
    (t2.status = 'DELETE' AND t1.userid IS NOT NULL) OR 
    (t2.userid IS NOT NULL AND t2.status != 'DELETE' AND t1.userid IS NULL) 
+0

Hi Riji,我很抱歉沒有清楚地解釋我自己。我試圖在每個表內* * Table1 *和* Table2 *中實現重複記錄,而不是通過將兩個表進行比較。我很抱歉,謝謝。 – gymcode

0

正如人們評論的 - 通過整理設置了大小寫敏感性。

至於檢查重複鍵 - 您可以使用EXISTS keyword
事情是這樣的:

WHEN EXISTS (SELECT * FROM Table3 WHERE userid=t1.UserID) 
    THEN -- DO WHATEVER 

但是,如果你願意禁用插入重複的ID,你應該通過設置主鍵/唯一索引,而不是去做。

編輯: 查詢發現在表1的重複:

Insert into Table3(userid,issue) 
SELECT t1.userid,'Duplicate in table1' 
FROM Table1 t1 
WHERE (SELECT COUNT(*) FROM Table1 _t1 WHERE _t1.userid = t1.userid) > 1 

對於表2中的查詢重複將是非常相似的。

+0

中的值匹配如何將你的代碼集成到我當前的代碼中,這樣如果在你的代碼中找到了重複的用戶標識符Table1和Table2,它會將用戶名,用戶名和問題(發現重複的用戶ID)插入到表3中? – gymcode

+0

讓我確定我明白你的意思:當且僅當Table1和Table2包含相同的用戶ID時,纔想將行插入到Table3中? – Svarog

+0

沒有。我只想在每個* Table1 *和* Table2 *本身內找到重複記錄時插入記錄。因此,這些問題將是'Table1中發現的重複記錄'和'Table2中發現的重複記錄'。謝謝 – gymcode

2

唯一的userId約束可能是最好的強制執行一個UNIQUE INDEX(應該有助於性能),但爲了確保您不要求重複輸入,這應該可以幫助您:我在表3中使用了LEFT JOIN來檢查值目前爲空(記錄不存在)

該代碼會忽略重複,而不是插入一條記錄

INSERT INTO Table3(UserId, Username, Issue) 
SELECT COALESCE(t1.UserId, t2.UserId), t1.UserName 
     , CASE 
      WHEN (t2.userid IS NULL AND t1.userid IS NOT NULL) 
       THEN 'User exists in t1 but not in t2' 
      WHEN (t2.status = 'DELETE' AND t1.userid IS NOT NULL) 
       THEN 'User Exists in t1, but status in t2 is DELETED' 
      WHEN (t2.userid IS NOT NULL AND t2.status != 'DELETE' AND t1.userid IS NULL) 
       THEN 'Non-Deleted user in t2 does not exist in t1' 
     END AS Issue 
    FROM table1 t1 
    FULL OUTER JOIN table2 t2 ON t1.userid = t2.userid 
    LEFT JOIN Table3 AS t3user ON t1.UserID = t3user.UserId 
    LEFT JOIN Table3 AS t3status ON t2.UserId = t3status.UserId 
    WHERE t3user.UserId IS NULL 
     AND t3status.UserId IS NULL 
     AND 
     (
      (t2.userid IS NULL AND t1.userid IS NOT NULL) 
      OR (t2.status = 'DELETE' AND t1.userid IS NOT NULL) 
      OR (t2.userid IS NOT NULL AND t2.status != 'DELETE' AND t1.userid IS NULL) 
     ) 

如果你想插入一條記錄表示重複,你可以使用以下命令:

INSERT INTO Table3(UserId, Username, Issue) 
SELECT COALESCE(t1.UserId, t2.UserId), t1.UserName 
     , CASE 
      WHEN (t3user.UserId IS NOT NULL OR t3Status.UserId IS NOT NULL) 
       THEN 'Duplicated userid found: "' + t1.UserId + '"' 
      WHEN (t2.userid IS NULL AND t1.userid IS NOT NULL) 
       THEN 'User exists in t1 but not in t2' 
      WHEN (t2.status = 'DELETE' AND t1.userid IS NOT NULL) 
       THEN 'User Exists in t1, but status in t2 is DELETE' 
      WHEN (t2.userid IS NOT NULL AND t2.status != 'DELETE' AND t1.userid IS NULL) 
       THEN 'Non-Deleted user in t2 does not exist in t1' 
     END AS Issue 
    FROM table1 t1 
    FULL OUTER JOIN table2 t2 ON t1.userid = t2.userid 
    LEFT JOIN Table3 AS t3user ON t1.UserID = t3user.UserId 
    LEFT JOIN Table3 AS t3status ON t2.UserId = t3status.UserId 
    WHERE 

     (
      (t2.userid IS NULL AND t1.userid IS NOT NULL) 
      OR (t2.status = 'DELETE' AND t1.userid IS NOT NULL) 
      OR (t2.userid IS NOT NULL AND t2.status != 'DELETE' AND t1.userid IS NULL) 
     ) 
+0

感謝您的解決方案。參考你的代碼,發現一個重複的用戶標識會發生什麼? – gymcode

+0

我想識別所有重複項而不是忽略它們,就像上面3次檢查的問題一樣,並給出重複鍵的問題,例如, '找到重複的用戶標識'。 – gymcode

+0

讓我知道如果這是你所希望的。 –