SQL Server/SSIS: How to query between two tables in different servers

Posted on 2013-10-30
Last Modified: 2016-02-11
Hi All,

I'm trying to run a fairly simple 'where' query between two tables residing on two different servers and I'm not sure which is the most efficient way (I eventually want to use the query in an SSIS SQL task to run on a daily basis):

1. Actually run the query between the two servers and if this is possible how do I write the syntax for it

2. Or perhaps pass the values in the column I'm basing my 'where' queries from to the new query so I don't have to query between two servers i.e. just have the values in the brackets instead of the sub query.

In this case I need to select all the values from Server2 and pass those values to a SQL query that will query Server1 like this:

  FROM [Server1\SQLEXPRESS].[DB1].[dbo].[Table1] as Tbl1
  WHERE Tbl.invoicecode IN (Values From Server 2)

Open in new window

The query I'm currently attempting to run is this:

  FROM [Server1\SQLEXPRESS].[DB1].[dbo].[Table1] as Tbl1
  WHERE Tbl.invoicecode IN (SELECT Tbl2.invoicenumber FROM [Server2].[DB2.[dbo].[Table2] as Tbl2)

Open in new window

And I get an error message like this:

Msg 7202, Level 11, State 2, Line 1
Could not find server 'Server1\SQLEXPRESS' in sys.servers. Verify that the correct server name was specified. If necessary, execute the stored procedure sp_addlinkedserver to add the server to sys.servers.

Question by:onesegun
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 66

Accepted Solution

Jim Horn earned 500 total points
ID: 39611198
>In this case I need to select all the values from Server2 and pass those values to a SQL query that will query Server1 like this:

If the end goal is to use this in SSIS, why not have create the first query as a source task in a data flow connected to server1, then build a lookup transform connected to server2 to do a lookup on the IN.. column?

Expert Comment

by:Sander Stad
ID: 39613487
You can create a linked server to make this query work.
This article helps you to create one:

Than simply use the name of the linked server in your query and you'll get the results
LVL 66

Expert Comment

by:Jim Horn
ID: 39616589
So .. how's it going?

Author Closing Comment

ID: 39621633
Hi Jim,

Your solution was perfect.

I used the lookup transformation to indeed accomplish the task simply. But I the only think I had to do was cast the specific lookup column otherwise it gave me an error.

Apart from that. Problem solved.




Author Comment

ID: 39621637
Hi sstad,

I didn't get the time to try your solution.

I'll read through it later on as it would be useful to know anyway.



Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

My client has a dictionary table. They're defining a list of standard naming convention. Now, they are requiring my team to provide us a mechanism how to match new incoming data with existing data in their system.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question