High level Steps:
- Install and configure FreeTDS to get required ODBC drivers
- Configure tns and listener services
- Create DB Link
Detailed Steps:
Step 1)
Download Freetds from the following link
ftp://ftp.freetds.org/pub/freetds/stable/freetds-patched.tar.gz
Install Freetds as root user.
Installation steps:
1) Unzip the freetds-patched.tar.gz compressed file.
2) Change directory to unzipped location and execute the following one by one.
3) ./configure –prefix=/usr/local/freetds
4) make
5) make install
Free TDS installation is completed.
Step 2)
Edit /etc/odbc.ini and add the following lines.
[<DB NAME>-connector]
Description = MS SQL connection to ‘<DB NAME>’ database
Driver = FreeTDS
Database = <MS SQL DB NAME>
Server = <MS SQL DB HOSTNAME>
UserName = <MS SQL DB Username>
Password = <MS SQL DB Password>
Trace = Yes
TraceFile = /tmp/freetds.log
TDS_Version = 7.0
Port = <MS SQL Port Number>
Example:
[MYDB-connector]
Description = MS SQL connection to ‘MYDB’ database
Driver = FreeTDS
Database = MYDB
Server = MYSQL-DB-HOST
UserName = sriram
Password = sql@123
Trace = Yes
TraceFile = /tmp/freetds.log
TDS_Version = 7.0
Port = 1433
Step 3)
Edit /etc/odbcinst.ini file and add the following lines. Make sure libstdsodbc.so is available at the specified location or change the location appropriately.
# Driver for MS_SQL
[FreeTDS]
Description = ODBC for Microsoft SQL
Driver = /usr/local/freetds/lib/libtdsodbc.so
UsageCount = 1
Threading = 2
Step 4)
1) Navigate to $ORACLE_HOME/hs/admin
2) Create a file with name initdg4mssql.ora like specified below. Following is the content of initdg4mssql.ora from my machine.
#############################################################################
# This is a sample agent init file that contains the HS parameters that are
# needed for the Database Gateway for ODBC
#
# HS init parameters
#
HS_FDS_CONNECT_INFO =MYDB-connector
HS_FDS_TRACE_LEVEL = 0
HS_FDS_SHAREABLE_NAME = /usr/lib64/libodbc.so
HS_RPC_FETCH_REBLOCKING= OFF
HS_FDS_FETCH_ROWS = 1
#HS_NLS_NCHAR = AMERICAN_AMERICA.US7ASCII
#HS_LANGUAGE = AMERICAN_AMERICA.US7ASCII
HS_LANGUAGE=AMERICAN_AMERICA.WE8ISO8859P1
HS_FDS_SUPPORT_STATISTICS=FALSE
#
# ODBC specific environment variables
#
set ODBCINI=/etc/odbc.ini
#
# Environment variables required for the non-Oracle system
#
#set
#############################################################################
Step 5)
Configure TNS and listener entries.
1) Navigate to $TNS_ADMIN.
2) Edit tnsnames.ora and append the following as per your environment. Assign an unused port to the new listener service
dg4mssql =
(DESCRIPTION=
(ADDRESS=(PROTOCOL=tcp)(HOST=<ORACLE HOST>)(PORT=<LISTENER PORT>))
(CONNECT_DATA=(SID=dg4mssql))
(HS=OK)
)
MYDB =
(DESCRIPTION=
(ADDRESS=(PROTOCOL=tcp)(HOST=<MS SQL DB HOST>)(PORT=<MS SQL SERVER PORT>))
(CONNECT_DATA=(SID=MYDB))
(HS=OK)
)
3) Edit listener.ora and append the following. You might face challenges related to configuring and starting listener service, so make sure you cross check the listner.ora and tnsnames.ora if you have any issues starting the listener.
LISTENER_dg4mssql =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = <ORACLE HOST>)(PORT = <LISTENER PORT>))
)
)
SID_LIST_LISTENER_dg4mssql =
(SID_LIST =
(SID_DESC =
(ORACLE_HOME= <ORACLE_HOME>)
(SID_NAME = dg4mssql)
(PROGRAM=dg4odbc)
)
)
Step 6) Create DB Link in Oracle Database for MS-SQL Database.
CREATE DATABASE LINK SQLSERVER.MYSERVER CONNECT TO “SRIRAM” IDENTIFIED BY “sql@123” USING ‘dg4mssql’;
Once the above steps are complete, you should be able to access MS-SQL data from Oracle database using database link.