2

Hello I have to pass a select from a database that is on an ip address to another (identical) database that is on a completely different IP, below the query how to pass to make the switch?

Sql Code:

/*Insert into database with same name into same table addres:: 172.16.50.98*/
Insert into 


/* select from database address: 172.16.50.96*/
SELECT IdUtente,Longitudine,Latitudine,Stato,DataCreazione
  FROM Quote.dbo.Marcatura
  where DataCreazione>'2019-01-08 18:37:28.773'
Hadi
  • 36,233
  • 13
  • 65
  • 124
riki
  • 1,502
  • 5
  • 17
  • 45

3 Answers3

0

Linked Server/ OpenQuery is the way to achieve this. have a look on this.

including parameters in OPENQUERY

KG18
  • 18
  • 3
0

If the data that's being imported isn't large and this won't be a reoccurring task a linked server would probably be the better option. Creating one through the SSMS GUI is easier if you haven't done this before, but an example of creating one using the SP_ADDLINKEDSERVER stored procedure through T-SQL is below. If your account doesn't have access to the other server the SP_ADDLINKEDSRVLOGIN stored procedure will need to be used to configure the linked server with an account that has the appropriate permissions on the source server, as well as database and any referenced objects. While using the linked server syntax (4 part name) is simpler and easier to read, I'd strongly recommend doing the insert with OPENQUERY instead if only one linked server will be used. This will execute the SQL on the source server, applying any filters there and only return the necessary rows, whereas the linked server syntax will return all the rows before performing the filtering. You can read more about the differences between the two here. You indicated the database name is the same on both servers, and this assumes the same for the table and schema names as well. Make sure to update these accordingly if they differ.

If a large volume of the data will imported or if this will be a regular process creating an SSIS package and setting this to run as a SQL Agent job will be the better approach. If you choose to go this route there are a number of things to consider, but the links below will help you get started. SQL Server Data Tools (SSDT) is where the packages can be developed. While not necessary, executing the packages from the SSIS Catalog, SSISDB, will be much more beneficial than just the using the file system. Either an OLE DB or SQL Server Destination can be used since the table that's being loaded to is on SQL Server, however a SQL Server Destination can only be used on a local database.

Linked Server:

 --Create linked server
--SQL product name and SQLNCLI11 provider for SQL Server
EXEC [MASTER].DBO.SP_ADDLINKEDSERVER @server = N'MyLinkedServer', @srvproduct=N'SQL', 
@provider=N'SQLNCLI11', @datasrc=N'ServerIPAddress'


--OPENQUERY insert
INSERT INTO Quote.dbo.Marcatura (IdUtente, Longitudine, Latitudine, Stato, DataCreazione)
SELECT 
    IdUtente, 
    Longitudine, 
    Latitudine, 
    Stato, 
    DataCreazione
FROM OPENQUERY(MyLinkedServer, '
SELECT 
    IdUtente, 
    Longitudine, 
    Latitudine, 
    Stato, 
    DataCreazione
FROM Quote.dbo.Marcatura')

SSIS:

userfl89
  • 4,610
  • 1
  • 9
  • 17
0

SSIS solution

I think this requires a very simple SSIS package to be achieved:

  1. Create two OLEDB Connection manager; one for each server
  2. Add a data flow task
  3. Inside the Data flow task addan OLEDB Source and OLEDB destination
  4. In the OLEDB source (172.16.50.98 connection manager) select SQL command as Access mode and use the following command:

    SELECT IdUtente,Longitudine,Latitudine,Stato,DataCreazione
      FROM Quote.dbo.Marcatura
      where DataCreazione >'2019-01-08 18:37:28.773'
    
  5. Map the source columns to the OLEDB destination (172.16.50.96 connection manager)

Helpful links

Hadi
  • 36,233
  • 13
  • 65
  • 124