Tải bản đầy đủ - 0 (trang)
4-4. Importing Oracle Data on an “Ad Hoc” Basis

4-4. Importing Oracle Data on an “Ad Hoc” Basis

Tải bản đầy đủ - 0trang

Chapter 4 ■ SQL Databases


INTO MyOracleTable

FROM OPENROWSET ('OraOLEDB.Oracle', 'MyOracle';'SCOTT';'Tiger',

'select * from SCOTT.EMP');

How It Works

As with the Microsoft Office products described in Chapter 1 (or indeed when accessing data from other SQL

Server instances), you can read and write data on an “ad hoc” basis from Oracle as a data source. To begin, let’s

see how to use OPENROWSET to run an “ad hoc” query. This can be somewhat slow, however, depending on your

environment. So I advise you to test this approach thoroughly to ensure that you can meet your SLAs if you

choose to implement it.

At a minimum, you will need the following:

The Oracle client installed on the SQL Server.

A network connection to the Oracle server (no Firewall issues!).

An Oracle user logon and password with rights to the data you wish to retrieve.

Ad hoc queries enabled on the SQL Server (as described in Recipe 1-4).

There are three elements that make up the OPENROWSET function:

The OLEDB provider (OraOLEDB.Oracle). I am using the Oracle provider.

The connection string ('MyOracle';'SCOTT';'Tiger').

The query ('select * from SCOTT.EMP').

The main thing to note is that the connection string contains (in this order):

The Oracle instance (MyOracle in the example given earlier).

The user ID (SCOTT).

The password (Tiger).

Note that all of these are separated by a semicolon, and the connection string is separated from the query by

a comma.

For more permanent two-way connections to an external database (using OLEDB), a linked server can

frequently be the best answer. To add an Oracle linked server, execute the following code snippet in SQL Server

Management Studio. I have placed all the code that follows this recipe in a single sample file


EXECUTE master.dbo.sp_addlinkedserver

@server = 'MyOracleDatabase',

@srvproduct = 'Oracle',

@provider = 'OraOLEDB.Oracle',

@datasrc = 'MyOracle';

EXECUTE master.dbo. sp_addlinkedsvrlogin

@rmtsrvname = 'MyOracleDatabase',

@useself = 'false',

@locallogin = NULL,

@rmtuser = 'SCOTT', -- The Oracle User name

@rmtpassword = 'Tiger'; -- The Oracle User password



Chapter 4 ■ SQL Databases

You can then use the standard four-part convention to read data from (or write to) tables and views in the

linked server.

Execute the following code snippet in SQL Server Management Studio to read the contents of an Oracle table

and import them into an SQL Server table:


INTO dbo.MyOracleTable

FROM MyOracleDatabase..SCOTT.EMP;

If you only want to import the table structure (or a the structure of a source view), then you can use the

following snippet:


INTO dbo.MyOracleTable

FROM MyOracleDatabase.. SCOTT.EMP

WHERE 1 = 0;

This will show you how SQL Server is mapping data types, which can be extremely useful.

Alternatively, you can use the OPENQUERY syntax to import data, like this:


INTO dbo.MyOracleTable

FROM OPENQUERY(MyOracleDatabase,'select * from SCOTT.EMP');

Another method of extracting data is to use the SQL Server 2005 (and up) feature EXEC...AT. At its most

simple, it uses a linked server to execute a command, like this:


So, assuming that you have a destination table structure, you can import data like this:

INSERT INTO dbo.MyOracleTable


This can then be tweaked very easily to accept parameters as part of the T-SQL, which allows you to

fine-tune the data selection:


INSERT INTO dbo.MyOracleTable


Finally, if the Oracle DBA has set up a PL/SQL stored procedure to return a dataset,

you can run:


EXEC (BEGIN SCOTT.MyPLSQLProcedurehere; END) AT MyOracleDatabase;

The external server could be case sensitive—indeed most Oracle implementations that I have seen have

been. So, to avoid potential problems, it is advisable to use the same case that the objects require when using

linked objects. More precisely, Microsoft Books Online (BOL) says that if the table and column names were

created in Oracle without quoted identifiers, use all uppercase names; otherwise, use the exact same case.



Chapter 4 ■ SQL Databases

Hints, Tips, and Traps

The AllowInProcess property must be set to 1 for the 'OraOLEDB.Oracle' provider

(right-click the provider in Server Objects ➤ Linked Servers ➤ Providers in SSMS to

change this).

You will need to set the Set RPC OUT server option for the linked server that you are using

for EXEC...AT to work. In SSMS right-click the linked server and select “Properties” to

modify this option.

The SQL Server Data Import Wizard can also import Oracle data. Since this tool was

described fairly exhaustively in Recipes 1-2 and 2-1, I will refer you to Chapters 1 and

2. When using it for Oracle source data, you need to specify that you will use an OLEDB

driver for Oracle (provided by either Microsoft or -preferably- Oracle).

To list all the available tables in the linked server, see Chapter 8.

■■Note  I am aware that using SELECT * in this recipe is bad programming practice. My reason for this approach

is that, in practice, you do not always have the details of the source data when connecting to external databases. By

returning all the available fields, you can at least get an initial idea of what the source data is like, and take a closer

look at the fields and data itself. When defining the definitive code for production, you should, of course, SELECT only

the fields that you want to import into SQL Server.

4-5. Migrating Multiple Oracle Tables and Views


You have tens—or hundreds—of Oracle source tables and/or views that need loading into SQL Server as part of

a migration.


Use SSMA (SQL Server Migration Assistant) for Oracle. The following explains how to prepare the SQL Server

objects and load data from an Oracle schema:


Download and install SSMS for Oracle. It is currently at



Run SSMA for Oracle (Start ➤ All Programs ➤ SQL Server Migration Assistant 2012 for



Click File ➤ New Project. Enter a name. Then, either enter or browse to find the

directory where the project will be stored.


Click the Connect to Oracle toolbar button. Enter the Oracle connection information.

You should see something like Figure 4-9.



Chapter 4 ■ SQL Databases

Figure 4-9.  Oracle connection for SSMA


In the Oracle Metadata Explorer pane, expand the schemas and then expand the

schema—or schemas—containing the tables you wish to migrate. Check the Tables

check box to select all the tables in the schema (or select the tables you wish to migrate).


Click Connect to SQL Server. Enter your SQL Server connection parameters. In the

SQL Server Metadata Explorer pane, select the destination database. You should see a

screen that is something like Figure 4-10.



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

4-4. Importing Oracle Data on an “Ad Hoc” Basis

Tải bản đầy đủ ngay(0 tr)