2011-10-13 84 views
0

有沒有人看到問題出在哪裏,這個SQL代碼?我用UNION在查詢的FROM行中不斷得到紅色下劃線。如果你能夠告訴我如何防止代碼重複,甚至更好!SQL NOT IN子句不起作用

DECLARE @collection_site_address_id INT; 
SET @collection_site_address_id = 
(
    SELECT TOP 1 
     client_address.addressid 
    FROM 
     dbo.ws_test_request 
     INNER JOIN client ON ws_test_request.collection_site_id = client.identifyingnumber 
     INNER JOIN client_address ON client.clientid = client_address.clientid 
    WHERE 
     sample_specimen_id = @sample_identifyingnumber 
     AND client_address.addresstypeid = 1 
) 

IF (

    (SELECT TOP 1 
     client_address.addressid 
    FROM 
     dbo.ws_test_request 
     INNER JOIN client ON ws_test_request.collection_site_id = client.identifyingnumber 
     INNER JOIN client_address ON client.clientid = client_address.clientid 
    WHERE 
     sample_specimen_id = @sample_identifyingnumber 
     AND client_address.addresstypeid = 1 
    ) 

    NOT IN (

     SELECT 
      [address].addressid 
     FROM 
      [address] 
      JOIN (
       SELECT 
        client_address.addressid, 
        client_address.addresstypeid, 
       FROM 
        dbo.fnClientRelatives(@clientid, 0, 1, 0) relatives 
        INNER JOIN client_address on client_address.clientid = relatives.clientid 
        LEFT OUTER JOIN client ON relatives.clientid = dbo.client.clientid 

       UNION 

       SELECT 
        contact_address.addressid, 
        contact_address.addresstypeid, 
       FROM 
        clientcontact 
        INNER JOIN contact_address ON contact_address.contactid=clientcontact.contactid and [email protected] 
        LEFT OUTER JOIN [contact] ON [clientcontact].contactid = [contact].contactid 
        LEFT OUTER JOIN [address] ON contact_address.addressid = address.addressid 
      ) AS client_addressexternal ON client_addressexternal.addressid = address.addressid 
     WHERE 
      client_addressexternal.addresstypeid IN (3,1) 
    ) 
) 
BEGIN 
    @collection_site_address_id = @default_collection_site_address_id 
END 

========

答:

有點調查後,它看起來像一個更有效的方式。您可以使用「IN」或「NOT IN」子句將表(結果集)與另一個表(結果集)進行比較。或者,您可以使用「EXISTS」或「NOT EXISTS」子句將標量(aka變量)與表(結果集)進行比較。

[標量] EXISTS([表/結果集])
[標量] IS NOT NULL AND NOT EXISTS([表/結果集])

OR

[表/結果集] IN(表/結果集])
[表/結果集] NOT IN(表/結果集])

DECLARE @collection_site_address_id INT; 
SET @collection_site_address_id = 
(
    SELECT TOP 1 
     client_address.addressid 
    FROM 
     dbo.ws_test_request 
     INNER JOIN client ON ws_test_request.collection_site_id = client.identifyingnumber 
     INNER JOIN client_address ON client.clientid = client_address.clientid 
    WHERE 
     sample_specimen_id = @sample_identifyingnumber 
     AND client_address.addresstypeid = 1 
) 

IF  
    @collection_site_address_id IS NOT NULL AND NOT EXISTS 
    (
     SELECT 
      [address].addressid 
     FROM 
      [address] 
      JOIN (
       SELECT 
        client_address.addressid, 
        client_address.addresstypeid 
       FROM 
        dbo.fnClientRelatives(@clientid, 0, 1, 0) relatives 
        INNER JOIN client_address on client_address.clientid = relatives.clientid 
        LEFT OUTER JOIN client ON relatives.clientid = dbo.client.clientid 

       UNION 

       SELECT 
        contact_address.addressid, 
        contact_address.addresstypeid 
       FROM 
        clientcontact 
        INNER JOIN contact_address ON contact_address.contactid=clientcontact.contactid and [email protected] 
        LEFT OUTER JOIN [contact] ON [clientcontact].contactid = [contact].contactid 
        LEFT OUTER JOIN [address] ON contact_address.addressid = address.addressid 
      ) AS client_addressexternal ON client_addressexternal.addressid = address.addressid 
     WHERE 
      client_addressexternal.addresstypeid IN (3,1) 
    ) 
BEGIN 
    SET @collection_site_address_id = @default_collection_site_address_id 
END 

回答

2

在字段列表中刪除多餘的逗號。例如替換

... 
SELECT 
    client_address.addressid, 
    client_address.addresstypeid, 
FROM 
... 

... 
SELECT 
    client_address.addressid, 
    client_address.addresstypeid 
FROM 
... 
+0

和我忘了BEGIN/END詞法組內的SET – MacGyver

1

你必須在那些選擇列表中的最後一欄後,尾隨逗號查詢。刪除它們,至少應該修復圍繞FROM關鍵字的紅線。

即:

SELECT 
    client_address.addressid, 
    client_address.addresstypeid -- <-- trailing comma removed 
FROM [...] 

UNION 

SELECT 
    contact_address.addressid, 
    contact_address.addresstypeid 
FROM [...]