Easily Import Your Excel File Into SQL Server
📝 Get my free SQL Cheat Sheets: https://tubular-dolphin-9ead68.netlify.app/go/30?video=g1JDAAKuscg&source=description
🎓 Go from zero to your first SQL query: https://tubular-dolphin-9ead68.netlify.app/go/60?video=g1JDAAKuscg&source=description
If you have data in an Excel file, you can easily import the Excel file into an SQL Server database.
In this video, I'll show you how to import an SQL Server file using SSMS into an SQL Server database.
⏱ TIMESTAMPS:
00:00 - Import into a table
03:38 - Select the data
Closed Caption:
Do you want to import an ExcelÂ
file into an SQL Server database?
I’ll show you how to easily do that in this video.
Here’s the Excel file we’re importing. We have a
few columns with a mix ofÂ
text, numbers, and dates.
Let’s load this into SQL Server.
I’ll demonstrate this in SSMSÂ
as it’s a popular editor.
Under your connection, expand theÂ
Databases entry. Right-click on
the database you want to import the dataÂ
into, select Tasks, and then Import Data.
This window will appear. You can click Next.
On this screen you can choose a data source.Â
It defaults to a .net framework option,
so we’ll need to change this to Microsoft Excel.
Now, click on Browse, and locateÂ
the file you want to import. I’ll
find that New Customers spreadsheet fileÂ
that I showed before, then click Open.
I get an error here which says the file is inÂ
use. This is because I have the Excel file open.
So, I’ll have to close this file first.
Back in SSMS, I can click OK, and thenÂ
click Open again. This time, it works.
The Excel File Path field is populated with theÂ
directory path and filename that we just selected.
For the Excel version, it automaticallyÂ
selects version 2007-2010, which is OK for us.
If your file has column names in the firstÂ
row, like my file does, leave this box checked.
Click Next.
You’ll see the DestinationÂ
screen. In the drop-down,
select SQL Server Native Client,Â
which is probably at the bottom.
You’ll see inputs for the server name,Â
authentication method, and database. The server
name and database defaults to the connectionÂ
and database we already selected, so I don’t
need to change that. I’m also using WindowsÂ
Authentication, so I can leave that selected.
Click Next.
On the next screen, you can either copyÂ
data from the source or use a script.
The terminology is a bit generic, as itÂ
caters for all kinds of data sources,
but for our spreadsheet we can justÂ
leave the first option selected.
So, click Next.
We can then see the SelectÂ
Source Tables and Views screen.
This will show a list of all worksheets that we
have in the file, and the tables thatÂ
this process will import the data into.
The target table here has defaulted to the sheetÂ
name, which is Sheet 1. I’d like to change that,
so we can do that here. I’llÂ
change it to new_customers.
There’s a button here called Edit Mappings.Â
You can click this and see the columns that
will be imported, as well as the data typesÂ
that will be used in the new table. This is
helpful to verify that all of the columnsÂ
are loaded, and the data types are correct.
It’s good to see here that the date value will beÂ
inserted into a datetime column and not a string.
Once you’ve seen this andÂ
made any changes, click OK.
Then I can click Next.
On the Save and Run Package screen,Â
leave the Run Immediately checked.
We don’t need an SSIS packageÂ
so we can leave this unchecked.
Click Next.
Finally, click Finish.
The import process will start,Â
and different steps will start
showing green ticks. My file isÂ
small, so it took a few seconds.
I also see the number of rowsÂ
that were imported here as well.
If you get any errors, you can go backÂ
to an earlier step in this import process
to adjust the settings, such as the table dataÂ
types or names, or look into the spreadsheet data.
For me, I’ll click Close.
The table has been created and theÂ
data has been imported. Let’s check it.
We can expand the database inÂ
the Object Explorer on the left,
then Tables. We should see it in the listÂ
here, but if we don’t, we can click Refresh.
Our new table is here, called new_customers.
Let’s write a query to getÂ
the data from this table.
I’ll write a quick SelectÂ
Star query on this table,
and I’ll make sure that this drop-downÂ
has the right database selected.
Click Execute.
I can see the results here, which areÂ
the same values from the spreadsheet.
The date values look valid andÂ
correct. The import process
has successfully determinedÂ
that this was a date column.
Sometimes this may not work correctly,
and it may be stored as a textÂ
value, but this time it has worked.
We can check the data types by expanding theÂ
table in the Object Explorer, then Columns.
We can see the data types here. There areÂ
a couple of NVARCHAR columns, a float,
and a datetime. If we want to use differentÂ
data types, we can alter them using SQL.
We’ve been able to import dataÂ
from an Excel spreadsheet into
our database. You’ll want toÂ
watch this video next to learn
how to export data from an SQL ServerÂ
database directly into an Excel file.
Thanks for watching.
Video Length: 04:58
Uploaded By: Database Star
Published: 9/5/2024
View Count: 41,491