Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6676003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:55:55+00:00 2026-05-26T03:55:55+00:00

Does anyone see where the issue is with this SQL code? I keep getting

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:55:56+00:00Added an answer on May 26, 2026 at 3:55 am

    Remove the extra commas in the field list. e.g. replace

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

    with

    ...
    SELECT 
      client_address.addressid,
      client_address.addresstypeid
    FROM
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

See here: http://code.google.com/p/ie7-js/ Does anyone have any experience or remarks about this javascript? Is
Does anyone have experience using the URL Rewrite Module (see here )? Can it
Does anyone know how I can check to see if a directory is writeable
Does anyone have metrics on the utility of formal Unit Testing? I see a
I see a lot of IoC frameworks for .Net and Java. Does anyone know
I ran this query on my SQL Server database: begin transaction; insert into [db].[a].[Table1]
Does anyone remember the XMP tag? What was it used for and why was
Does anyone know how to get IntelliSense to work reliably when working in C/C++
Does anyone have any recommendations of tools that can be of assistance with moving
Does anyone use have a good regex library that they like to use? Most

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.