0

I have to retrive the data from one service based sql server to another service based sql server

select * from servernameA.db_name.dbo.table_name 
where column in (select column from servernameb.db_name.dbo.table_name)
Paritosh
  • 11,144
  • 5
  • 56
  • 74
Thiru
  • 64
  • 1
  • 1
  • 10

1 Answers1

0

So what's the problem? You can do it.

Just a little performance improvement in your query with DISTINCT.

select * from servernameA.db_name.dbo.table_name 
where column in (select DISTINCT column from servernameb.db_name.dbo.table_name)

For your reference:
Query across multiple databases on same server
Selecting data from two different servers in SQL Server

------------Edit----------------
How about this? I haven't tried this yet. Give this a try.

// connectionString is the connection string for the ServerA..DB1
using (var conn = new SqlConnection(connectionString))
{
     conn.Open();
     // provide commandText as the SQL query having the full name of Server2..DB2
     using (var command = new SqlCommand(commandText, conn))
     {
         using (var reader = command.ExecuteReader())
         {
              while(reader.Read())
              {
                  // read the result
              }
         }
     }
}
Paritosh
  • 11,144
  • 5
  • 56
  • 74
  • I want to run C# So Please give me the sample code for C# – Thiru Jan 22 '18 at 04:20
  • Thanks For your reply but .... Give me sample code for how to select data from one sql server to another sql server .......... select * from servernameA.db_name.dbo.table_name where column in (select column from servernameb.db_name.dbo.table_name) – Thiru Jan 22 '18 at 10:22