Thursday, 30 October 2014

Linking and Accessing files among two databases on Oracle, DB2 and MSSQL



Lets assume we have two databases as 'FIRST', 'SECOND' with 'first' and 'second' are the schema's for each databases and we have a table 'SECFILE' IN 'SECOND' database. So to access the 'SECFILE' from FIRST database we follow the below steps.

Oracle :

From First database (FIRST): Create database link from first database to connect to second database (SECOND)

SQL> CREATE PUBLIC DATABASE LINK ORALINK CONNECT TO second identified by second using 'SECOND';
Database link created.

From First database : Create the synonym for the table in second database as below.

SQL>  CREATE SYNONYM TESTF FOR SECFILE@ORALINK;
Synonym created.

Now we can access the table 'SECFILE' in 'SECOND' database from 'FIRST' database by referring the synonym name like

From First database:

desc TESTF;
select * from TESTF;


DB2:

Same way we can do in DB2 as below. Assuming same database names and tables,

From 'FIRST' database:

db2 => CREATE WRAPPER drda
DB20000I  The SQL command completed successfully.
db2 => CREATE SERVER DB2LINK TYPE DB2/UDB VERSION 10.1 WRAPPER DRDA AUTHORIZATION "second" PASSWORD "second123" OPTIONS (ADD NODE 'SECOND_N', ADD DBNAME 'SECOND')
DB20000I  The SQL command completed successfully.
db2 => CREATE USER MAPPING FOR SECOND SERVER DB2LINK OPTIONS (REMOTE_AUTHID 'second', REMOTE_PASSWORD 'second123')
DB20000I  The SQL command completed successfully.
db2 => CREATE NICKNAME TESTF FOR DB2LINK.second.SECFILE
DB20000I  The SQL command completed successfully.
db2 =>

From First database:

describe table TESTF;
select * from TESTF;


MSSQL :

Create a synonym for the table in 'SECOND' database as below.

CREATE SYNONYM TESTF FOR SECOND.second.SECFILE

Additionally need to provide privileges to 'first' of 'FIRST' database to access 'SECOND' database and back and fro.




















From First database:

select * from dbo.TESTF

No comments:

Post a Comment