Tải bản đầy đủ - 0 (trang)
1-2. Importing Data from Excel

1-2. Importing Data from Excel

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

CHAPTER 1 ■ Sourcing Data from MS Office Applications



Solution

Run the SQL Server Import and Export Wizard and use it to guide you through the import process.

Here is the process to follow:

1.



In SQL Server Management Studio, right-click a database (preferably the one into

which you want the data imported), click Tasks ➤ Import Data (see Figure 1-1).



Figure 1-1.  Launching the Import/Export Wizard from SSMS

2.



Skip the splash screen. The Choose a Data Source screen appears.



3.



Select Microsoft Excel as the data Source, and enter or browse for the file to import.

Be sure to select the Excel version that corresponds to the type of source file from the

pop-up list, and specify if your data includes headers (see Figure 1-2).



4

www.it-ebooks.info



CHAPTER 1 ■ Sourcing Data from MS Office Applications



Figure 1-2.  Choosing a Data Source in the Import/Export Wizard

4.



Click Next. The Choose a Destination dialog box appears (see Figure 1-3).



5

www.it-ebooks.info



s



Figure 1-3. Choosing a Destination in the Import/Export Wizard

5.



Ensure that the destination is SQL Server Native Client, that the server name is

correct, and that you have selected the right destination database (CarSales_Staging

in this example) and the authentication mode which you are using (with the

appropriate username and password for SQL Server authentication).



6.



Click Next. The Specify Table Copy or Query dialog box appears (see Figure 1-4).



6

www.it-ebooks.info



CHAPTER 1 ■ Sourcing Data from MS Office Applications



Figure 1-4.  Specifying Table Copy or Query in the Import/Export Wizard

7.



Accept the default “Copy data from one or more tables or views”.



8.



Click Next. The Select Source Tables or Views dialog box appears (see Figure 1-5).



7

www.it-ebooks.info



CHAPTER 1 ■ Sourcing Data from MS Office Applications



Figure 1-5.  Choosing the Source Table(s) in the Import/Export Wizard

9.



Select the worksheet(s) to import.



10. Click Next. The Save and Run Package dialog box appears (see Figure 1-6).



8

www.it-ebooks.info



CHAPTER 1 ■ Sourcing Data from MS Office Applications



Figure 1-6.  Running the Import/Export Wizard package

11. Ensure that Run Immediately is checked and that Save SSIS Package is not checked.

12. Click Next. The Complete the Wizard dialog box appears (see Figure 1-7).



9

www.it-ebooks.info



CHAPTER 1 ■ Sourcing Data from MS Office Applications



Figure 1-7.  Completing the Import/Export Wizard

13. Click Finish. The Execution Results dialog box appears. Assuming that all went well,

the data has loaded successfully (see Figure 1-8).



10

www.it-ebooks.info



CHAPTER 1 ■ Sourcing Data from MS Office Applications



Figure 1-8.  Successful execution using the Import/Export Wizard

14. Click Close to end the process.



How It Works

There will probably be times when your sole aim is to get a load of data from an Excel spreadsheet into an SQL

Server table as fast as possible. Now, when I say “fast,” I do not only mean that the time to load is very short, but

that the time spent setting up the load process is minimal and that the job gets done without going to the bother

of setting up an SSIS package, defining a linked server, or writing T-SQL using OPENROWSET to do the job. This is

where the SQL Server Import and Export Wizard (DtsWizard for short) comes into its own. An extra inducement

is that the guidance provided by the DtsWizard application can be invaluable if you only import spreadsheet data

infrequently.



11

www.it-ebooks.info



CHAPTER 1 ■ Sourcing Data from MS Office Applications



As this is the first time that the Import and Export Wizard is explained in this book, I have tried to make the

explanation as complete as possible. The advantage is that you will find many of the techniques explained here

useable for other types of source data, too.

You should use the SQL Server Import and Export Wizard:





When you need to import data from an Excel spreadsheet into an SQL Server table just

once.







When you do not intend to perform the action regularly or frequently.







When you rarely import Excel data, you don’t want to get lost in the arcane world of SSIS

and/or rarely used SQL commands. You want the data imported fast.







When you want to import data from multiple worksheets or ranges in the same workbook.



Assuming that your Excel data is clean and structured like a data table, then the data will load. It can either

be transferred to a new table (or new tables), which are created in the destination database with the same

name(s) as the source worksheets, or into existing SQL Server tables. You can decide which of these alternatives

you prefer in step 8.



Hints, Tips, and Traps





If you are working in a 64-bit environment, the 32-bit version of the Import/Export

Wizard runs from SSMS. To force the 64-bit version to run, choose Start ➤ All Programs

➤ Microsoft SQL Server 2012 ➤ Import and Export Data (64 bit). Should you need to

install the 32-bit version of the wizard, select either Client Tools or SQL Server Data Tools

(SSDT) during setup.







If you plan on using the DtsWizard.exe frequently, add the path to the executable to your

system path variable—unless it has already been added.







You can also launch the SQL Server Import and Export Wizard executable by entering

Start ➤ Run ➤ DtsWizard.exe (normally found in C:\Program Files\Microsoft SQL

Server\110\DTS\Binn), or by double-clicking on the executable in a Windows Explorer

window (or even a command window).



1-3. Modifying Excel Data During a Load

Problem

You want to import data from an Excel spreadsheet, but need to perform a few basic modifications during the

import. These could include altering column mapping, changing data types, or choosing the destination table(s),

among other things.



Solution

Apply some of the available options of the SQL Server Import and Export Wizard. As we are looking at options

for the SQL Server Import and Export Wizard, I will describe them as a series of “mini-recipes,” which extend the

previous recipe.



■■Note  Step numbers in the sections to follow refer to the process in Recipe 1-2.



12

www.it-ebooks.info



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



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

1-2. Importing Data from Excel

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

×
x