You tagged your question with many RDBMS.
My answer is about SQL Server.
Since I know the password and ip address, things should go very easy
like this: insert into db2.tbl2 select * from db1.tbl1
And this is exactly the syntax you can use in case of linked servers
. You incapsulate connection string in the definition of linked server, then the only thing that changes is linked server name in your code:
insert into db2.tbl2 select * from server1.db1.tbl1
example of linked server setup:
EXEC master.dbo.sp_addlinkedserver @server = N's_2005', @srvproduct=N'', @provider=N'SQLNCLI', @datasrc=N'127.0.0.1,8851'
go
EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N's_2005',@useself=N'False',@locallogin=NULL,@rmtuser=N'login1',@rmtpassword='*****'
go
select top 5 * from [s_2005].[Northwind].[dbo].[Employees]
If you want other solutions, you can use OPENROWSET (Transact-SQL)
providing the whole connection string in your INSERT
every time you want to access remote server:
select a.* from openrowset('SQLNCLI', 'Server=127.0.0.1,8851;Trusted_Connection=no;uid=login1;pwd=*****;', 'select top 5 * from [Northwind].[dbo].[Employees]') as a
The third option is SSIS
where you incapsulate connection strings into SourceConnection
and DestinationConnection
Here is a picture of the same result using linked server
and openrowset
:
