SQL Server 2008 Essential Training
Illustration by Mark Todd

Importing CSV files into SQL Server


SQL Server 2008 Essential Training

with Simon Allardice

Start your free trial now, and begin learning software, business and creative skills—anytime, anywhere—with video instruction from recognized industry experts.

Start Your Free Trial Now

Video: Importing CSV files into SQL Server

If the data that you want to import is in comma separated values or indeed in anything that's just a regular text delimited file-- in fact here I've got to some very straightforward values, very similar to the last time, but they are separated with the vertical bar. I can also bring this into SQL Server although the way of doing it does have quite a few different options than doing it with Excel. So I'm going to go ahead and open up my Import and Export Data Wizard. I'm going to use the 32-bit one.
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. Using subqueries
      9m 33s
    9. Joining multiple tables together
      8m 0s
    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

please wait ...
Watch the Online Video Course SQL Server 2008 Essential Training
6h 54m Beginner Dec 15, 2010

Viewers: in countries Watching now:

In SQL Server 2008 Essential Training, Simon Allardice explores all the major features of SQL Server 2008 R2, beginning with core concepts: installing, planning, and building a first database. Explore how Transact-SQL is used to retrieve, update, and insert information, and gain insight into how to effectively administer databases. The course also covers features outside SQL Server's database engine, including technologies that have grown up around it: SQL Server Reporting Services and Integration Services. Exercise files are included with the course.

Topics include:
  • Using T-SQL (Transact-SQL)
  • Managing databases with SQL Server Management Studio
  • Understanding database normalization
  • Using SELECT statements
  • Building indexes
  • Monitoring database size and integrity
  • Backing up and restoring databases
  • Creating functions and stored procedures
  • Managing database permissions
  • Creating and formatting reports
  • Adding charts to reports
  • Creating and executing a simple SSIS package
Business Developer IT
SQL Server
Simon Allardice

Importing CSV files into SQL Server

If the data that you want to import is in comma separated values or indeed in anything that's just a regular text delimited file-- in fact here I've got to some very straightforward values, very similar to the last time, but they are separated with the vertical bar. I can also bring this into SQL Server although the way of doing it does have quite a few different options than doing it with Excel. So I'm going to go ahead and open up my Import and Export Data Wizard. I'm going to use the 32-bit one.

The source I'm going to pick here is Flat File Source. And when I select that, not surprisingly it's going to ask me to browse to that file, which is currently saved as a CSV on my desktop. And once I do that we have a very different screen for determining what that data is. If I click on the Columns section on the left, it will take a look and think yes, this column delimiter seems to be a vertical bar, and if you select that, this is what the date is going to look like and it looks pretty good. Although it does seem to say that the first name, last name, address, and city are values. Now it's not the case. I actually did one of those to be the column names.

If I look back on my general section I'll see that I do have a checkbox. The column names are in the first data row. So we click back to the Columns, and yes we seem to have that now sorted correctly. Interestingly, if I go to the Advanced section, it will tell me the way it's interpreting these values. So it's breaking down all the different columns and saying FirstName it thinks is a string that's 50, and LastName as a string that's 50, and Address as a string that's 50. And in fact you'll see this a lot. It's pretty much going to assume that everything as a string of 50, even a zip, and email, and phone, and that's okay, but it might lead us into issues.

For example, I'm pretty sure I have some e-mail addresses that are longer than 50 characters. There is a button-down here called Suggest Types. Now what that will do is look through some of this semi-mapped data and make a prediction that if something is purely numeric, well maybe it should be an integer. I'm not a big fan of using Suggest Types, because I tend to find that it gets a bit too pessimistic. So for example, if I were to click Suggest Types here, it might go through all the last names and if the largest one that it finds is 12 characters, it's going to assume that the widest this column needs to be is 12 characters.

That may be true, but that may lead you into issues later on. So I'm going to leave that alone. If I click Next, we then say where are we copying this data to. And I'll get a chance to map it again in just a minute, but I'm copying it to my current SQL Server database, which I'm going to represent with just the dot, the period. I'm using Windows Authentication and that should give me the drop-down list where I can select my new custom database. Click Next. It's telling me it's going to import a table.

That's importing from the CSV data and it's going to create a table called csvdata. I could choose to change that if I wanted, but let me just leave that right now. Once again, I'm going to click Edit Mappings. Now we'll see here that everything is now being listed as a varchar with a maximum size of 50. That may work. That may not. In fact I'm pretty sure that certain things like my address should be at least a hundred and that my e-mail should be at least a hundred as well.

I'm going to click OK, click Next, tell it that yes I want to run immediately. click Finish and go. And we see that we've got an error here, we've got some messages popping-up hat's saying, unfortunately the output column Email failed because truncation occurred. And the truncation row disposition on the output column specifies failure on truncation. What does this mean? Again it's to do with how we're specifying this copy, this import, to work.

I'm going to click Back quite a bit here and go back to where it thinks these mappings occur, and in fact the problem is probably on Email. Wll that's were it said the problem was. Right now it does think that it only needs 50 characters to do its mapping. I'm going to say no, you need a hundred characters or truncation will occur. I'm going to click Next and go ahead keeping the same data. What I've got an issue with now is with Mappings. So we'll take a quick scan of those, now it did say address was different, but it didn't complain about address so I'll just leave that.

Click Finish and try this again, and that looks a lot better. We now have a warning here. The total number of rows processed is 101. There is a partial row at the end of the file. That's quite common message to get. You can check that later, but we do seem to have a hundred rows transferred. So I'm going to close that, open up Management Studio, and just double-check that everything looks okay. And it does seem to have brought everything in. We do have the columns named correctly.

We do have a hundred rows. It does not seem to have created anything in that last one, so we're looking good. Once again of course we could change the definitions of that. But if you know that you are immediately needing to go in and change this information, you should try and do a lot of it as you are mapping it before you actually create the table during the import. But that's the process of bringing in a CSV file.

Find answers to the most frequently asked questions about SQL Server 2008 Essential Training .

Expand all | Collapse all
please wait ...
Q: I'm having problems installing the free Express R2 version of SQL Server on Windows XP. I tried 64-bit and 32-bit versions. In the videos, the author installs from a DVD. Do I need to do the same?
A: While the author installs from a DVD, it's not strictly necessary. There certainly shouldn't be a problem installing the Express edition from a regular download. That's the way it's intended to be installed.

If you're using Windows XP, the only officially supported version is the 32-bit version. However, you do need to make sure that your Windows XP install is completely up-to-date and patched, with XP Service Pack 3 installed. (See http://msdn.microsoft.com/en-us/library/ms143506.aspx#Express32 for formal requirements.)

It's not unusual for the install process to take a while, and with older operating systems like XP, you'll often have to back it out and try again, as usually there's a bunch of prerequisites that need to be installed. (Like the .NET Framework 3.5 SP1, the correct version of Windows Installer, etc.)
Q: The link to the installer for the AdventureWorks sample database, as shown in the Chapter 2 movie "Installing sample databases," no longer works. Where can I find the installer?
A: Microsoft has reorganized its site. The sample files are still there, but they're a bit harder to find. To install them:

1) Visit http://msftdbprodsamples.codeplex.com/.
2) Click the link to "SQL Server 2008 R2 OLTP."
3) Click the AdventureWOkrs2008R2 Data File link and agree to the conditions to download the MDF file.
4) Move the MDF file to your SQL Server Directory, usually located at C:\Program Files\Microsfot SQL Server\MSSQL 10_50.MSSQLSERVER\MSSQL\DATA.
5) Open the SQL Sever Management Studio and connect to your instance using an account with administrative privileges.
6) Attach the sample database by right-clicking the Databases folder in the Object Explorer and choosing Attach from the pop-up menu.
7) Click the Add button in the next menu and navigate to the MDF file in the Locate Database Files window that appears. Select it and click OK.
8) Remove the reference to the log file in the "AdventureWorks2008R2" database details: pane by selecting the Log entry and clicking removing.*
9) Click OK to return to SQL Server Management Studio and complete the attachment process.

*MDF files are the "data" files for SQL Server databases. They often come along with LOG files (ldf files). This one didn't so we need to REMOVE the reference to the non-existent log file. Select the second row in the lower section (it should say File Type: Log and Message: Not Found) and click the REMOVE button.

For an illustrated version of these instructions (with screenshots), click here for a PDF version.
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.

Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now Already a member? Log in

* Estimated file size

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.

Mark all as unwatched Cancel


You have completed SQL Server 2008 Essential Training.

Return to your organization's learning portal to continue training, or close this page.

Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

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.