Quantcast
Channel: CodeSection,代码区,SQL Server(mssql)数据库 技术分享 - CodeSec
Viewing all articles
Browse latest Browse all 3160

Transferring Data Between SQL Server 2014 and Oracle 11g Databases

$
0
0

By:Rick Dobson || Related Tips:More > Other Database Platforms

Problem

I need a demonstration that illustrates step-by-step instructions for copying table rows between a 64-bit SQL Server 2014 database server and an Oracle 11g database server. I need to know how to perform the transfer going both ways - namely, from a SQL Server 2014 database to an Oracle 11g database as well as from Oracle 11g to SQL Server 2014.

Solution

It is common for BI developers to use SSIS for transferring data between two heterogeneous database servers , such as SQL Server and Oracle. Unfortunately, Visual Studio for SQL Server 2014 was never released by Microsoft in a version that supported transfers between 64-bit versions of SQL Server and Oracle databases. Because Oracle 11g is a 64-bit database, it requires 64-bit transfer technology. See these two links for additional documentation and user commentary about SSIS not supporting 64-bit transfers between SQL Server 2014 and Oracle 11g.

https://blogs.msdn.microsoft.com/analysisservices/2014/04/02/sql-server-data-tools-business-intelligence-for-visual-studio-2013-ssdt-bi/ http://stackoverflow.com/questions/24989187/64-bit-microsoft-sql-server-data-tools

There is, however, a workaround. You can use aLinked Server from within a 64-bit SQL Server 2014 instance to enable the transfer of table rows between SQL Server 2014 and Oracle 11g. The Linked Server in SQL Server 2014 pointing at an Oracle 11g database permits the execution of DDL statements to create and drop tables as well as DML statements forselecting andinserting data from one database to another.

Here's a top level summary of the demonstration in this tip for moving data from a 64-bit SQL Server 2014 database server to an Oracle 11g database and back again.

Create and verify the operation of a Linked Server in a 64-bit SQL Server 2014 server pointing at an Oracle 11g database. Gain access to a table with rows you will be copying between the servers. For example, download the backup file and restore the sample database for this tip. Run SQL code via the SQL Server Linked Server to create a target table in Oracle to receive rows of data from SQL Server. Populate the freshly created Oracle table with data from SQL Server and verify that the copied values in Oracle match the original source data in SQL Server. Copy data from Oracle to SQL Server and verify the copied values from Oracle match the original source data from SQL Server. Create a SQL Server Linked Server to Access Oracle

A prior mssqlTips.com article, Creating a SQL Server 2014 Linked Server for an Oracle 11g Database , provides step-by-step instructions for creating, configuring, and verifying a Linked Server in SQL Server 2014 that points to an Oracle 11g database. The tip demonstrates how to unlock an account for the HR schema in the sample database that ships with Oracle 11g and also reveals how to use the Linked Server in a simpleSELECT query statement. The linked server's name in the tip is OrclDB.

After setting up the Linked Server, you should be able to verify that the following query returns 3 rows of data from the EMPLOYEES table in the HR schema of the sample database. This result set verifies the Linked Server is interacting properly with the sample Oracle database. See the original article on a Linked Server for Oracle 11g to view the contents of the result set.

-- No database context is required for this query
-- but the OrclDB linked server must be created as described in
-- Creating a SQL Server 2014 Linked Server for an Oracle 11g Database tip
-- Confirm the successful creation of the OrclDB linked server
-- It returns 3 rows
-- DO NOT RUN SUBSEQUENT QUERIES UNTIL THIS QUERY SUCCEEDS
-- Required caps for schema and table names
SELECT TOP 3 * FROM OrclDB..HR.EMPLOYEES Setup Demonstration to Move Data

Download the backup file ( SSandOracleDataExchange.bak ) associated with this tip. The backup file is for a database with just one table containing 200,000 rows with three columns named FIRST_NAME, LAST_NAME, and BIRTH_DATE. The table was created in a database named SSandOracleDataExchange. The name of the table is SQL_SERVER_DATA_FOR_ORACLE.

You can use the following script to restore the SSandOracleDataExchange database on your 64-bit SQL Server 2014. The script conditionally removes a copy of the database if it already exists on theserver.

-- Conditionally drop and then restore source database
-- with data for copying between SQL Server and
-- Oracle database servers
USE [master]
GO
IF EXISTS(select * from sys.databases where name='SSandOracleDataExchange')
DROP DATABASE SSandOracleDataExchange
RESTORE DATABASE SSandOracleDataExchange FROM
DISK = N'C:\SSandOracleDataExchange\SSandOracleDataExchange.bak'

After restoring the database, you can run a few queries to familiarize yourself with the table within the database.

The first query below shows column values for three rows from the SQL_SERVER_DATA_FOR_ORACLE table (see the result set in the screen shot below). First and last name values are derived from cross joining a subset of US census data on first and last names. Birth dates were randomly assigned within an arbitrary range to the names. The second query confirms that there are 200,000 rows in the SQL_SERVER_DATA_FOR_ORACLE table. The third query verifies that all 200,000 rows in the source table are unique by the combination of FIRST_NAME, LAST_NAME, and BIRTH_DATE column values. USE SSandOracleDataExchange
GO
-- Show values for 3 rows from the
-- SQL_SERVER_DATA_FOR_ORACLE table
-- in the SSandOracleDataExchange database
SELECT TOP 3 *
FROM [SQL_SERVER_DATA_FOR_ORACLE]
-- There are 200000 rows in the
-- SQL_SERVER_DATA_FOR_ORACLE
SELECT COUNT(*) Count_of_rows_in_SQL_SERVER_DATA_FOR_ORACLE
FROM [SQL_SERVER_DATA_FOR_ORACLE]
-- Each of the 200000 rows has a distinct
-- set of FIRST_NAME, LAST_NAME, and BIRTH_DATE values
SELECT COUNT(*) Count_of_distinct_rows_in_SQL_SERVER_DATA_FOR_ORACLE
FROM
(
SELECT
DISTINCT
FIRST_NAME
,LAST_NAME
,BIRTH_DATE
FROM [SQL_SERVER_DATA_FOR_ORACLE]
) for_distinct_rows
Transferring Data Between SQL Server 2014 and Oracle 11g Databases
Create Target Table in Oracle to Receive Rows from SQL Server The next step in the demonstration is to create a table in the Oracle 11g sample database to receive rows from the SQL_SERVER_DATA_FOR_ORACLE table in SQL Server. Both SQL Server and Oracle use a CREATE TABLE statement for creating a new database. This statement will fail if the name for the table you try to create already exists in the same schema of the database. Therefore, you should check if the name for new table is assigned to an existing table in the schema before invoking the C

Viewing all articles
Browse latest Browse all 3160

Trending Articles