Tải bản đầy đủ - 0 (trang)
7-29. Exporting the Result of an MDX Query in SSIS

7-29. Exporting the Result of an MDX Query in SSIS

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

Chapter 7 ■ Exporting Data from SQL Server



Solution

Use an SSIS data flow task to export the data. I’ll show you how.

1.



Create a destination table called CubeOutput already set up to hold the source data,

created as follows (C:\SQL2012DIRecipes\CH07\tblCubeOutput.sql):

CREATE TABLE CarSales_Staging.dbo.CubeOutput

(

ClientName NVARCHAR(250),

SalePrice NVARCHAR (250),

CostPrice NVARCHAR (250)

);

GO



2.



Create a new SSIS package. Add an ADO.NET connection manager named

OLAP_Source, configured as follows:

ADO.NET Provider:



.NET Providers for OLEDB \Microsoft OLEDB

Provider for Analysis Services 11.0



Server or File Name:



localhost (or your SSAS instance, if on another box)



Initial Catalog:



CarSales_OLAP (for my examples—use your

SSAS database for your data).



3.



Click All on the left of the dialog box, scroll up to Extended Properties, and enter

FORMAT = TABULAR.



4.



Add an OLEDB connection manager named OLEDB_Destination, configured as

follows:

OLEDB Provider:



SQL Server Native Client 11.0



Server or File Name:



ADAM02 (Substitute with your SQL Instance)



Initial Catalog:



CarSales (or your database)



417

www.it-ebooks.info



Chapter 7 ■ Exporting Data from SQL Server



5.



Add a Data Flow task. Double-click to edit. In the Data Flow pane, add an ADO.NET

source. Configure as follows:

ADO.NET Connection

Manager:



OLAP_Source



Data Access Mode:



SQL Command



SQL Command Text:



WITH MEMBER

[Measures].[SalePrice] AS CSTR([Measures].[Sale Price])

MEMBER [Measures].[CostPrice] AS CSTR([Measures].[Cost Price])

SELECT

{[Measures].[SalePrice], [Measures].[CostPrice]}

ON COLUMNS,

NONEMPTY(EXCEPT([Dim Clients].[Client Name].MEMBERS,

[Dim Clients].[Client Name].[All]))

ON ROWS

FROM [Car Sales DW]



6.



Confirm your modifications with OK. Click OK to disregard the warning message.



7.



Add an OLEDB destination task, and connect the source task to it. Configure as follows:



8.



OLEDB Connection Manager:



OLEDB_Destination



Data Access Mode:



Table or View – Fast Load



Name of Table or View:



CubeOutput



Map the (peculiarly named) source columns to the destination columns, and click OK.



How It Works

Exporting the result of an MDX query is virtually identical to the process described in the previous recipe, so

I will not re-explain the principles here. I accept that calling an MDX pass-through query an SQL Command

is counter-intuitive, yet nonetheless it works—and so this is how you pass MDX to the SSAS cube. You need to

remember that MDX can only export the data as a Unicode text stream, so you will then have to CAST or CONVERT

all fields to other data types in your SQL, or use a view to overlay the output table and convert the field types to

the appropriate data types. In my opinion, it is advisable to create the destination table with columns sufficiently

large (at least the first time you export the data) to prevent the export from failing.



Hints, Tips, and Traps





A warning triangle will remain in the source component. This is because the driver is having

difficulties with the measures output from the MDX query. Nonetheless, it will work.







If you do not use named members and convert the output to string, you will have

difficulties (sometimes, but not always) when exporting the data.



418

www.it-ebooks.info



Chapter 7 ■ Exporting Data from SQL Server







If you are exporting to a text file, then add the column headers manually to the

destination file, and uncheck “Overwrite data in the file”.







If the complexity of the required data conversion annoys you too much, then you can

always use OPENROWSET in an OLEDB source (using the code described previously) for an

SQL Server source. The downside to this, in practice, is the complexity of permissions and

the requirement for ad hoc queries to be enabled.



7-30. Exporting Data to Other Relational Databases

Using T-SQL

Problem

You want to export data into a third-party RDBMS using T-SQL.



Solution

Use OPENROWSET, OPENDATASOURCE, or a linked server. The following steps show how to export data to Oracle using

OPENROWSET.

1.



Configure a linked server using the script given in Recipe 4-4.



2.



Create a table called Client_Dest on the SCOTT schema of the destination Oracle

database:

CREATE TABLE "SCOTT"."Client_Dest"

(

"ID" INTEGER,

"ClientName" VARCHAR2(150),

"Country" VARCHAR2(50),

"Town" VARCHAR2(50),

"County" VARCHAR2(50),

"Address1" VARCHAR2(50),

"Address2" VARCHAR2(50),

"ClientType" VARCHAR2(20),

"ClientSize" VARCHAR2(10)

);



3.



Use the following SQL snippet to insert data into the Client_Dest Oracle table:

INSERT INTO OPENROWSET(

'OraOLEDB.Oracle', 'ORCL';'SYSTEM';'Me4B0ss',

'select ID,ClientName,Country,Town from SCOTT.Client_Dest')

SELECT

ID,ClientName,Country,Town

FROM

dbo.Client;



How It Works

Thanks to the maturity of the OLEDB and .NET providers for a range of relational databases, exporting data

to Oracle, DB2, Sybase, Informix, and PostgreSQL (to name but a few) is remarkably simple—providing that

the required drivers, and where necessary, client software are correctly installed. I feel that it is difficult not to

stress this point forcefully enough. In my experience, the vast majority of cross-database import and export



419

www.it-ebooks.info



Chapter 7 ■ Exporting Data from SQL Server



problems boil down to these two basic elements. Once they are in place and tested, the rest is a piece of cake. So

I encourage you to ensure that these fundamental aspects of the link are set up correctly before proceeding with

any attempt at exporting data.

However, even establishing the linking infrastructure correctly is not always a guarantee of flawless execution.

You need a sound knowledge of the data types of the destination tables and any limitations imposed by the

relational structure—that is, any unique constraints, foreign keys, non-nullable fields, and so forth. This enables you

to ensure that the data that you are exporting is suitable for the destination data structure and loads successfully.

The OPENROWSET command needs the parameters shown in Table 7-9.

Table 7-9.  OPENROWSET Parameters for Oracle Export



Description



Example



Oracle OLEDB Provider



OraOLEDB.Oracle



Oracle Address in TNSNames.Ora file



ORCL



Oracle Logon



SYSTEM



OraclePassword



Me4B0ss



Destination Fields



select ID,ClientName,Country,Town



I use Oracle as the external data destination for the examples in this recipe because as far as I can ascertain,

it seems to be the most frequently used database for exporting data from SQL Server. The techniques described

shortly will work with most other relational sources, and it is (with a few rare exceptions) only a case of altering

the provider name for the destination database. Other database providers and some of their quirks are described

in detail in Chapter 4.

Using OPENROWSET and OPENDATASOURCE is not difficult when using either the Microsoft or the Oracle

providers. These examples use the more modern Oracle provider.

If you prefer to use OPENDATASOURCE, then the following SQL snippet will insert data into the Client_Dest

Oracle table:

SELECT * FROM OPENDATASOURCE('OraOLEDB.Oracle', 'Data Source = ORCL;User

ID = SYSTEM;Password = Me4B0ss')..SCOTT.EMP;

Assuming that you have a linked server named MyOracleDatabase already set up (as described in Recipe 4-4),

then inserting data into a linked server is a standard INSERT...SELECT operation, only using four-part notation,

something like this:

INSERT INTO MyOracleDatabase.CarSales.dbo.Client_Dest

(

ID

,ClientName

,Country

,Town

)

SELECT

ID

,ClientName

,Country

,Town

FROM dbo.Client;



420

www.it-ebooks.info



j



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

7-29. Exporting the Result of an MDX Query in SSIS

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

×