New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Importing Excel files into SQL Server

From: SQL Server 2008 Essential Training

Video: Importing Excel files into SQL Server

So I'm going to go ahead and take a straightforward Excel spreadsheet and import that into SQL Server. This is the spreadsheet that I have at the moment. It's a list of names, addresses, and zips and email, just some contact information. I can see that the first row does seem to contain the column names for this, so that might be useful in just a moment. But that's what I want. I just want it in my database, because it's way too easy to mess it up when it's an Excel. So I'm going to open up the SQL Server 2008 R2, Import and Export Data.

Importing Excel files into SQL Server

So I'm going to go ahead and take a straightforward Excel spreadsheet and import that into SQL Server. This is the spreadsheet that I have at the moment. It's a list of names, addresses, and zips and email, just some contact information. I can see that the first row does seem to contain the column names for this, so that might be useful in just a moment. But that's what I want. I just want it in my database, because it's way too easy to mess it up when it's an Excel. So I'm going to open up the SQL Server 2008 R2, Import and Export Data.

I'll open up the 32-bit one, so it should be all I would need here. I'm going to click Next. Select that my data source is Microsoft Excel. It's going to ask me for the file path, which is just out here on my desktop. And it's going to ask me for the Excel version and I think it's 97-2003. If there is a problem with that it will probably tell me in just a moment. And I'll leave this checked. Yes the first row has column names. I'll click next. It's asking me now where is it going to. Well, this really means the current SQL Server, SQL Server Native Client 10.0.

But I could be copying to anywhere. Again, all these management tools don't have to be running on the local machine of a SQL Server instance, so it's just asking for the server name. Well, I'm just going to put in the period here. I could type in the name of my machine as well. That would work. If I click the drop-down box it will try and see if there's any more that it knows about and there won't be right now. So that's just my local machine. It doesn't matter which one I use. I'm going to say yes, I'm talking to it Using Windows Authentication.

And because I've named that if I click the drop-down box now I should see a list all of the databases there, and yes I do. I'm going to select my TwoTreesTest custom database that I have. I would even have the option to make a new database at this time. Now I'm going to do that. I'm going to click Next and it's going to ask what we are copying. Now this is a little bit over-complex for what I'm doing right now, because it's saying are we copying data from one or more tables or views? And this is a bit more useful when say you're coping from an Access database and you only want to copy certain pieces of it.

I'm going to leave the default selected and click Next and it's going to tell me this is the stuff that it knows about. Apparently, it's got a source of data called exceldata$. And it will create something called dbo.exceldata$. Not quite what I'm after, but I can always change that later. The interesting button here is going to be this one down at the bottom, Edit Mappings. That's going to tell me what it's going to try and do with the columns in the Excel data source and what it's going to map it to.

Now right now, they're named quite well. We've got the FirstName, LastName, Address, City, State, and so on. I'll keep those names and it will even tell me here what it's going to do with them, what data types it's going to use when it creates this table. And mostly they're listed as nvarchar, so variable-length Unicode fields that are nullable and is this generic size of 255. That's not quite what I was looking for and this one definitely looks wrong. Yes my Zip is numeric, but it's certainly not a float.

That's an estimate based on the data that existed in that spreadsheet. So I could change that. And again, depending on what I'm doing, whether the postal codes I know might have letters in them or they might have dashes, even if I'm using US postal codes. So I could change that to an nvarchar as well. But I see that what it did there was actually change it to max, which is not what I really want. It doesn't need 2 gigabytes of space to go and in fact, 20 would probably do it just fine. You will occasionally find that it does this, where it doesn't accept something that you're typing into.

And there is a couple of different ways as you could do it. So for example, I've changed the data type and suddenly seemed to take my 20. So I'm going to now change it back. It just seems to be a little of strange behavior I've seen a couple of times. And it's up to you whether you want to change them to be Nullable or not. I'm going to leave this as is. Click OK. Then I'm going to click Preview, which just shows me, yes this is the data that it's going to try and fetch from Excel. It all seems to be mapped correctly. I'll click OK. Click Next.

It's giving me a little prompt here that it's kind of letting me know that it thinks that zip was a double, and I'm wanting to store in an nvarchar. So is there a conversion process going on? And yes there is. Well, what if there's an error, what do I want to do? Do I want to do something special, do I want to fail the whole thing or just ignore it and move on? If the data was going to be truncated, so if say I decided to move it into a varchar with a maximum length of two digits, we'd have a lot of truncation going on.

I'm just going to use the Global Settings, which are these ones down here. Any error makes it fail and any truncation makes it fail. I'll click Next, and I'll tell it yes, Run immediately. The other option would be to save this as an SSIS Package, which is SQL Server Integration Services. Again, we'll talk about more of that later. But just to let you know, even a simple import that you're doing is using integration services. I'm going to say Run immediately. Click Next. It gives me a summary and click Finish.

It runs through a whole bunch of stuff. We even get a message here that 101 rows were transferred. I'm going to close this, go back into SQL Server Management Studio, connect to the database, and not surprisingly we should find now in our tables this rather ugly looking dbo.exceldata$, but I could open it up and say Select Top 1000 Rows. And we're bringing back all our content. I could right-click the table right now, and just rename it to something more specific.

Let's imagine these were coming from customers, so I called it the Customer table. And of course, I can even right-click and hit Design, jump into this, play around with the data types, even add a primary key, perhaps even generate one. This is the basic procedure of how you would import from an Excel spreadsheet.

Show transcript

This video is part of

Image for SQL Server 2008 Essential Training
SQL Server 2008 Essential Training

74 video lessons · 35787 viewers

Simon Allardice
Author

 
Expand all | Collapse all
  1. 2m 21s
    1. Welcome
      1m 19s
    2. Using the exercise files
      1m 2s
  2. 17m 58s
    1. SQL Server core concepts
      9m 4s
    2. SQL Server editions
      3m 8s
    3. Applications included with SQL Server
      5m 46s
  3. 26m 1s
    1. Preparing for installation
      3m 44s
    2. Creating service accounts
      2m 33s
    3. Installing SQL Server
      11m 42s
    4. Post-installation checks
      3m 9s
    5. Installing sample databases
      4m 53s
  4. 13m 35s
    1. Introduction to SQL Server Management Studio
      8m 7s
    2. Introduction to SQL Server Books Online
      3m 6s
    3. SQL Server system databases
      2m 22s
  5. 1h 26m
    1. Planning your database
      9m 39s
    2. Creating a SQL Server database
      4m 7s
    3. Creating tables
      7m 51s
    4. Data types in SQL Server
      12m 25s
    5. Defining keys
      8m 9s
    6. Creating default values
      4m 39s
    7. Creating check constraints
      2m 25s
    8. Creating unique constraints
      4m 34s
    9. Introduction to relationships and foreign keys
      9m 51s
    10. Creating relationships in SQL Server Management Studio
      8m 14s
    11. Database normalization
      11m 47s
    12. Creating computed columns
      3m 10s
  6. 23m 11s
    1. Using the SQL Server Import and Export Wizard
      3m 58s
    2. Importing Excel files into SQL Server
      6m 11s
    3. Importing CSV files into SQL Server
      5m 27s
    4. Importing Access databases into SQL Server
      7m 35s
  7. 55m 29s
    1. Introduction to Transact-SQL
      3m 43s
    2. Using SELECT statements
      7m 16s
    3. Changing the default database
      2m 21s
    4. Creating conditions in SQL
      8m 10s
    5. Sorting your output
      3m 23s
    6. Using aggregate functions
      7m 12s
    7. Finding unique values
      2m 14s
    8. Joining multiple tables together
      8m 0s
    9. Using subqueries
      9m 33s
    10. Viewing execution plans
      3m 37s
  8. 19m 36s
    1. Writing INSERT statements
      5m 47s
    2. Writing UPDATE statements
      4m 38s
    3. Writing DELETE statements
      2m 54s
    4. Using the OUTPUT clause to return inserted keys and GUIDs
      6m 17s
  9. 32m 52s
    1. Introduction to SQL functions
      6m 26s
    2. Using SQL configuration functions
      2m 14s
    3. Using string functions
      7m 26s
    4. Using date functions
      6m 27s
    5. Creating user-defined functions
      10m 19s
  10. 28m 46s
    1. Introduction to stored procedures
      4m 23s
    2. Creating stored procedures
      11m 23s
    3. Introducing transactions
      4m 23s
    4. Creating transactions
      8m 37s
  11. 16m 39s
    1. Understanding and creating indexes
      6m 32s
    2. Monitoring and rebuilding indexes
      6m 0s
    3. Monitoring database size and integrity
      4m 7s
  12. 11m 41s
    1. Creating backups
      4m 21s
    2. Creating differential backups and using backup compression
      3m 40s
    3. Restoring databases
      3m 40s
  13. 17m 40s
    1. Introduction to SQL Server security and permissions
      5m 54s
    2. Adding a Windows user to the database
      5m 7s
    3. Creating SQL Server logins and switching authentication modes
      6m 39s
  14. 36m 41s
    1. Introduction to SQL Server Reporting Services
      2m 52s
    2. Connecting to the Report Manager
      4m 29s
    3. Using Report Builder
      12m 4s
    4. Formatting values in reports
      4m 17s
    5. Adding indicators to reports
      5m 11s
    6. Adding charts to reports
      3m 54s
    7. Working with report security
      3m 54s
  15. 24m 41s
    1. Introduction to SQL Server Integration Services (SSIS)
      1m 57s
    2. Using Business Intelligence Development Studio (BIDS)
      6m 59s
    3. Creating and executing a simple SSIS package
      7m 35s
    4. Importing packages into SQL Server Management Studio
      3m 21s
    5. Scheduling jobs with SQL Server Agent
      4m 49s
  16. 31s
    1. Goodbye
      31s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold

Are you sure you want to delete this note?

No

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.