Query structure for WHERE EXISTS for Linked Server with Lookup Table?

techresearch7777777 1,796 Reputation points
2024-04-18T19:33:57.04+00:00

Hello, I have a Linked Server which my target is a VIEW and also have a local DB Lookup TABLE which contains just one Column with multiple records/values.

I would like to get all of the records from the Linked Server VIEW which has multiple Columns (including the matching Column in my local DB Lookup TABLE) but just the records which contains the values within my Lookup TABLE.

I heard WHERE EXISTS performs better than an IN clause for this type of filter... can anyone provide me the structure template of this query please?

Thanks in advance.

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,744 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. CosmogHong-MSFT 23,246 Reputation points Microsoft Vendor
    2024-04-19T05:59:05.1233333+00:00

    Hi @techresearch7777777
    Try this:

    SELECT *
    FROM   LinkServer_View B
    WHERE  NOT EXISTS (SELECT 1 
                       FROM   localDB_table A 
                       WHERE  A.matching_Column = B.matching_Column)
    

    Best regards,

    Cosmog Hong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".