Click here to Skip to main content
15,890,043 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hi All,
I want to do a query in which one of the value is from a column which is in a different server. My query is executing in a server and one of the values to be fetched from a table in a different server.Can anyone suggest me some way to do this?

Thanks in advance.
Posted

Your query can be achieve using following steps


1.add link server thro sql server.


XML
sp_addlinkedserver '<Server Alias>', '', 'SQLNCLI', NULL, NULL, 'SERVER=<IP>', NULL
sp_addlinkedsrvlogin '<Server Alias>', 'false', NULL, '<username>', '<password>'



just write the statement

select * from [LINKSERVERALIASNAME].[DATABASENAME].[ObjectName].[TABLENAME]
 
Share this answer
 
Comments
Subramaniam.C 26-Aug-10 5:56am    
Thank u Shabeer.Its working fine..
AspDotNetDev 26-Aug-10 12:46pm    
FYI, I deleted your duplicate answer.
shanawazway 20-Sep-10 2:22am    
Hi Shabbir,this Shahnawaz. I read ur answer, but I have not it understood properly.
Can u please explain me how to execute it and (From .Net Visual Studio OR SQL Visual Studio)

Can u explain me below two lines, and from where it should run ?
sp_addlinkedserver '<server alias="">', '', 'SQLNCLI', NULL, NULL, 'SERVER=<ip>', NULL
sp_addlinkedsrvlogin '<server alias="">', 'false', NULL, '<username>', '<password>'
If it's a completely different server, I'm not sure it's possible. If it's a different DB on the same server, you can refer to it in your SQL. SQL Server 2008 onwards can run .NET code, which may make it possible.
 
Share this answer
 
Comments
AspDotNetDev 26-Aug-10 12:46pm    
2005 can run .NET code as well.
Well you can take a look at OPENROWSET

I give you a simple example to fetch records from other server like

SQL
SELECT a.* FROM OPENROWSET('MSDASQL.1', 'DRIVER=SQL Server;SERVER=192.168.2.2;UID=sa;PWD=;DATABASE=CBOS',
'SELECT * FROM MASTER') as a


Try google for more example
 
Share this answer
 
Comments
Christian Graus 26-Aug-10 2:55am    
Wow - cool. I had no idea this existed ( but then, no project i've worked on has been insane enough to split data between two databases )
Goutam Patra 26-Aug-10 3:51am    
<has been="" insane="" enough="" to="" split="" data="" between="" two="" databases="">
Thats right. I was just trying to answer the question. I really don’t bother about OP’s DB design

I only use this to import data from Access, Excel files.
Not sure it's the best approach, but...

You can create a web service to allow you to get data from one SQL Server instance.

You can then create a CLR UDF to call that web service from another SQL Server instance.

Kinda cheating, but it should work.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900