Solved

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

Posted on 2013-10-30
5
863 Views
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:

SELECT *
  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:

SELECT *
  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.
Thanks,

OS
0
Comment
Question by:onesegun
  • 2
  • 2
5 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
Comment Utility
>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?
0
 
LVL 9

Expert Comment

by:Sander Stad
Comment Utility
You can create a linked server to make this query work.
This article helps you to create one: http://sqlserverplanet.com/dba/how-to-add-a-linked-server

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

Expert Comment

by:Jim Horn
Comment Utility
So .. how's it going?
0
 

Author Closing Comment

by:onesegun
Comment Utility
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.

Thanks,

OS

PS http://www.bidn.com/blogs/timmurphy/ssas/1492/lookup-with-different-data-type
0
 

Author Comment

by:onesegun
Comment Utility
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.

Thanks,

OS
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Over the last 2 years, I have been working on SSIS 2008. Really the tough tasks in SSIS are to deploy packages and pass parameters (Values from outside package). The latter is certainly a headache for developers, particularly for me. We had to ma…
In couple weeks ago, I encountered an extremely difficult problem while deploying 2008 SSIS packages to a new environment (SQL Server 2014 standard).  My scenario is: We have one C# application that is calling 2008R2 SSIS packages to load text fi…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now