简体   繁体   中英

SQL NOT IN clause isn't working

Does anyone see where the issue is with this SQL code? I keep getting red underscores around the FROM lines in the query with the UNION. If you're able to also tell me how I can prevent the duplication of code, even better!!

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 clientcontact.clientid=@clientid
                    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

========

Answer:

After a bit of investigation, it looks like a more efficient way. You can either compare a table (result set) to another table (result set) using the "IN" or "NOT IN" clause. Or you can compare a scalar (aka variable) to a table (result set) using the "EXISTS" or "NOT EXISTS" clause.

[scalar] EXISTS ([table/result set])
[scalar] IS NOT NULL AND NOT EXISTS ([table/result set])

OR

[table/result set] IN ([table/result set])
[table/result set] NOT IN ([table/result set])

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 clientcontact.clientid=@clientid
                    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

Remove the extra commas in the field list. eg replace

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

with

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

You've got trailing commas after the last column in the select lists of those queries. Remove them and that should fix the red lines around the FROM keywords, at least.

Ie:

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

UNION

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM