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

4-14. Importing Sybase ASE Data on an “Ad Hoc” Basis

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

Chapter 4 ■ SQL Databases



Solution

Use T-SQL and OPENROWSET—or set up a linked server.

The following code inserts data from Sybase ASE into an SQL Server table using OPENROWSET

(C:\SQL2012DIRecipes\CH02\SybaseOpenRowset.sql):

SELECT *

INTO MySybaseASETable

FROM OPENROWSET ('ASEOLEDB', 'WIN-491Q1DHMHOF:5000';'Adam';'Me4B0ss',

'select au_lname from dbo.authors');



How It Works

The Sybase ASE OLEDB provider is very well-behaved and allows you to import data using OLEDB into SQL

Server using T-SQL. At a minimum, you will need the following:





A functioning network connection to the Sybase ASE server.







The ASEOLEDB provider installed on the SQL Server.







A Sybase ASE 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 (ASEOLEDB).







The connection string ('WIN-491Q1DHMHOF:5000';'Adam';'Me4B0ss'). You have to use

your own connection parameters, of course.







The Query (‘select au_lname from dbo.authors’). Here I am using a standard Sybase

ASE sample database. You can use any database to which you have the appropriate rights.



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





The Sybase ASE instance (WIN-491Q1DHMHOF:5000 in the example provided). Note that

this is the server name (or the Sybase ASE Server name if one was given at installation)

and the port (which you can roughly think of as an SQL Server instance), which was

configured at installation for Sybase ASE.







The user ID (Adam). You will use an appropriate user, of course.







The password (Me4B0ss).



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

query by a comma. Inevitably, you can use INSERT INTO ... SELECT if you prefer, rather than create a table

using SELECT...INTO as I did here.

There are several valid reasons for using OPENROWSET to import data. In no particular order, and without any

pretentions to completeness, they are as follows:





You can use all the power of the SELECT clause to choose the columns to import.







You can filter imported data using a WHERE clause.







You can sort the imported data using an ORDER BY clause.



You can also set up a linked server to Sybase ASE if you want. I’ll explain the easiest way to do this.



229

www.it-ebooks.info



Chapter 4 ■ SQL Databases



1.



Create an ODBC System DSN using the Adaptive Server Enterprise ODBC driver,

configured as shown in Figure 4-39.



Figure 4-39.  Configuring an ODBC DSN for Sybase ASE

2.



Run the following T-SQL to create a linked server using the MyASEODBC System DSN

as the data source (all three following snippets are in

C:\SQL2012DIRecipes\CH02\SybaseLinkedServer.sql):



230

www.it-ebooks.info



Chapter 4 ■ SQL Databases



EXECUTE master.dbo.sp_addlinkedserver

@server = 'MySybaseASEDatabase',

@srvproduct = 'Sybase ASE',

@provider = 'MSDASQL',

@datasrc = 'MyASEODBC' ;

3.



Run the following T-SQL snippet to define security for ASE access:

EXECUTE master.dbo.sp_addlinkedsrvloginSP_ADDLINKEDSRVLOGIN Ã

@rmtsrvname = 'MySybaseASEDatabase',

@useself = 'false',

@locallogin = NULL,

@rmtuser = 'Adam', -- The Sybase ASE User name

@rmtpassword = 'Me4B0ss' ; -- The Sybase ASE User password



You can then use four-part notation to import data from the Sybase ASE server, like this:

SELECT *

INTO SybaseTable

FROM MySybaseASEDatabase.pubs2.dbo.authors;



Hints, Tips, and Traps





It is very probable (though not inevitable) that Sybase ASE will be case sensitive. So if you

get an error message with an OPENROWSET query, you can always start by verifying that you

are using the correct case for all objects to which you are referring.







When defining a Sybase ASE ODBC System DSN, be sure to use the 64-bit ODBC

administrator if you are in a 64-bit environment. Otherwise, you will create a DSN that

you can see and test—but not use for a linked server connection.







Yes, you are right, SELECT * is very bad practice. My excuse here is that when faced with

third-party databases, you may want to use this in order to return all the columns if you

do not have the metadata available.



4-15. Importing Sybase ASE Data on a Regular Basis

Problem

You want to import data into SQL Server from Sybase ASE over your network on a regular basis.



Solution

Use SSIS and the Sybase ASE OLEDB provider. The following explains how.

1.



In an SSIS package, right-click in the Connection Managers pane. Select New OLEDB

Connection.



2.



Click New.



3.



Select the Sybase OLEDB Provider from the pop-up list of installed providers.



4.



Enter the server name, followed by a colon and the port number.



231

www.it-ebooks.info



Chapter 4 ■ SQL Databases



5.



Enter the username and password.



6.



Select the database (Initial Catalog) you wish to connect to.



7.



Click OK.



8.



Extend and finish the SSIS package with an appropriate destination.



You can then import data from Sybase ASE into SQL Server.



How It Works

Once again, the art here is to ensure that the Sybase OLEDB provider is installed and functioning correctly.

With the provider up and running, connecting to Sybase ASE and selecting data for import is easy. Virtually the

only trick to know is that you must add a colon ( : ) and the Sybase ASE port number to the server name for the

connection manager to work properly.



Hints, Tips, and Traps





You can also use the Sybase ODBC provider (assuming that it is installed on the SQL

Server). You will have to create a DSN first, and then use this with the ODBC source in a

Data Flow task.



4-16. Loading Teradata Data

Problem

You have data stored in a Teradata database that you need to load into SQL Server Enterprise edition.



Solution

Download and install the Attunity SSIS Connector for Teradata. Then load the data using an SSIS Data Flow task.

1.



Download the Attunity SSIS Connector for Teradata that corresponds to your

environment (32-bit or 64-bit). They are currently available from

www.microsoft.com/en-gb/download/details.aspx?id=29284.



2.



Double-click the .msi file and install the connector. You can modify some of the

installation parameters, but accepting the defaults is nearly always easier.



3.



In an SSIS package, right-click inside the Connection Managers tab. Select MSTERA

in the Add SSIS Connection Manager dialog box. The dialog box should look like

Figure 4-40.



232

www.it-ebooks.info



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

4-14. Importing Sybase ASE Data on an “Ad Hoc” Basis

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

×