Tải bản đầy đủ - 0 (trang)
1-3. Modifying Excel Data During a Load

1-3. Modifying Excel Data During a Load

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

CHAPTER 1 ■ Sourcing Data from MS Office Applications



Querying the Source Data

To filter the source data, at step 6, choose the “Write a query to specify the data to transfer”option. You see the

dialog box in Figure 1-9.



Figure 1-9.  Specifying a source query to select Excel data

Here you can enter an SQL query to select the source data. If you have a saved an SQL query, you can browse

to load it. Note that you use the same kind of syntax as when using OPENROWSET, as described in Recipe 1-4. When

writing queries, note that worksheet data sources have a “$” postfix, but ranges do not.



Altering the Destination Table Name

In step 8, you can change the destination table name to override the default worksheet or range name.



13

www.it-ebooks.info



CHAPTER 1 ■ Sourcing Data from MS Office Applications



Replacing the Data in the Destination Table

Another available option is to replace all the data in the destination table. Of course, this will only affect an

existing table—if the table does not exist, then DTSWiz creates one whichever option is selected.

To do this, at step 8 from earlier, click Edit Mappings. The Column Mappings dialog box appears

(see Figure 1-10).



Figure 1-10.  Editing column mappings in the Import/Export Wizard

Selecting Delete Rows in Destination Table truncates the destination table before inserting the new data.

This option is only available if the file exists already.



Enabling Identity Insert

The Column Mappings dialog box (see Figure 1-10) also lets you enable identity insert, and insert values into an

SQL Server Identity column. Simply check the “Enable identity insert” check box.



14

www.it-ebooks.info



CHAPTER 1 ■ Sourcing Data from MS Office Applications



Adjusting Column Mappings

The Column Mappings dialog box also lets you specify which source column maps to which specific destination

column. Simply select the required destination column from the pop-up list—or  if you do not wish to

import the data for a specific column.



Changing Field Types for New Tables

You can—within the permissible limits of data type mappings—change both field types and lengths/sizes.

Altering the size of a text field avoids the default 255-character import text field length. Changing the field type

modifies the field type during the data load.

If you are creating a new table, then the new table is created with the newly defined field types and sizes.

However, be warned, altering data types will not alter the data, and any types or data lengths that you choose

must be compatible with the source data, or the load will fail.



Creating an SQL Server Integration Services (SSIS) Package

from the Import/Export Wizard

An extremely useful feature of the Import/Export Wizard is the ability to create a fully-fledged SSIS package from

the parameters that you have set when configuring your import. This is probably no surprise, as the Import/

Export Wizard is, essentially, an SSIS package generator. While the packages that it generates are not perfect, they

are a good—and fast—start to an ETL creation process.

To generate the SSIS package, simply check the Save SSIS Package box in the Save and Execute

Package dialog box (see step 9, Figure 1-6). You are prompted for a file location. The package is created when

you click Finish.



How It Works

Having stressed (I hope) that DtsWizard is a fabulous tool for rapid, simple data imports, I wanted to extend your

understanding by showing how versatile a tool the DtsWizard can prove to be in more complex import scenarios.

This is due to the wide range of options and parameters that are available to help you to fine-tune Excel imports.



Hints, Tips, and Traps





If you are using SQL Server 2005, then you will find a couple of minor differences in the

Choose a Data Source dialog box shown in Figure 1-2.







Clicking on any messages in the message column of the final dialog box (see Figure 1-8) is

invaluable for getting error messages should there be any problems.



1-4. Specifying the Excel Data to Load During an Ad-Hoc Import

Problem

You want to import only a specific subset of data from an Excel spreadsheet by defining the rows to load or

filtering the source data.



15

www.it-ebooks.info



s



Solution

Use SQL Server’s OPENROWSET command as part of a SELECT statement. This lets you use standard T-SQL to subset

the source data. For example, you can run the following code snippets:

1.



In the CarSales_Staging database, create a destination table named LuxuryCars

defined as follows (C:\SQL2012DIRecipes\CH01\tblLuxuryCars.Sql):

CREATE TABLE dbo.LuxuryCars

(

InventoryNumber int NULL,

VehicleType nvarchar(50) NULL

) ;

GO



2.



Enable remote queries, either by running the Facets/Surface Area Configuration tool

(or the Surface Area Configuration tool directly in SQL Server 2005), or running the

T-SQL given in the following

(C:\SQL2012DIRecipes\CH01\AllowDistributedQueries.Sql):

EXECUTE master.dbo.sp_configure 'show advanced options', 1;

GO

reconfigure ;

GO

EXECUTE master.dbo.sp_configure 'ad hoc distributed queries', 1 ;

GO

reconfigure;

GO



3.



Run the following SQL snippet

(C:\SQL2012DIRecipes\CH01\OpendatasourceInsertACE.Sql):

INSERT INTO CarSales_Staging.dbo.LuxuryCars (InventoryNumber, VehicleType)

SELECT CAST(ID AS INT) AS InventoryNumber, LEFT(Marque, 50) AS VehicleType

FROM OPENDATASOURCE(

'Microsoft.ACE.OLEDB.12.0',

'Data Source = C:\SQL2012DIRecipes\CH01\CarSales.xls;Extended Properties = Excel 12.0')...

Stock$

WHERE MAKE LIKE '%royce%'

ORDER BY Marque;



How It Works

There are times when quick access to the data in an Excel worksheet is all you need. This could be because you

need to perform a quick SELECT...INTO or INSERT INTO...SELECT using Excel as the data source. In this case,

firing up SSIS—or even running the Import Wizard (see Recipe 1-2)—to load data can seem like overkill. This

is where judicious application of SQL Server’s OPENDATASOURCE and OPENROWSET commands as part of a SELECT

statement can be extremely useful.



16

www.it-ebooks.info



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

1-3. Modifying Excel Data During a Load

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

×
x