Import Excel into SQL Server: Types, IDENTITY and Failed Rows

Want to import data from Excel file to SQL Server ?

Try Withdata FileToDB, a desktop Excel to SQL Server converter for Windows, MacOS, and Linux, Convert Excel to SQL Server, Easy and Fast.

Download Withdata FileToDB Download FileToDB

SQL Server is the strictest of the three about what a spreadsheet may hand it. The import
wizard is four steps; everything that goes wrong happens in the mapping and in the first bad row:

What goes wrong Why Fix
The imported ids are not the ids you sent The column is an IDENTITY: SQL Server generates its own sequence and ignores the values Import into a plain int column, or SET IDENTITY_INSERT … ON for the load
Chinese text arrives as ? The column is varchar (single-byte code page) rather than nvarchar Use nvarchar/nvarchar(max) for anything that is not plain ASCII
Dates lose their seconds / come back as 1900-01-01 datetime rounds to 3 ms and starts in 1753; a bad conversion falls back to the default datetime2 for new tables; check the mapping for the date column before running
Bulk load data conversion error (truncation) for the whole file BULK INSERT/bcp reject the batch on the first conversion error unless MAXERRORS is raised Stage into nvarchar(max) columns, then CAST into the real table

Import data from Excel to SQL Server in 4 steps

Choose Excel file -> Set SQL Server table -> Preview SQL Server table -> Import Excel to SQL Server

Choose SQL Server and logon.

convert Excel file to SQL Server table - Choose SQL Server and logon

Click “Wizard – 1 File To 1 Table” for “Excel to SQL Server”.

convert Excel file to SQL Server table - task window

Select the “Excel” file type.

Import file data to database table - select file type

then show the wizard.

Step #1. Open an Excel file.

Import Excel Data To SQL Server - open Excel file

Step #2. Select a SQL Server table and config fields.

Load Data From Excel To SQL Server - select table

Step #3. Preview SQL Server table.

Transfer Data From Excel To SQL Server - preview

Step #4. Import Excel to SQL Server.

Convert Data From Excel To SQL Server - import data


More about Excel to SQL Server converter

Excel type to SQL Server type

Two decisions matter more than the rest: nvarchar instead of varchar, and
datetime2 instead of datetime. The rest is sizing.

In the sheet In SQL Server Watch out for
Text with accents or CJK nvarchar(n), nvarchar(max) varchar stores one byte per character in a code page; anything outside it is lost as ?
Date and time datetime2(0) or date datetime rounds to .000/.003/.007 and cannot hold dates before 1753.
Money decimal(12,2) Not float; and money is a legacy type with its own quirks.
Numbers from the sheet int, bigint A cell formatted as text may arrive as '1,234' and fail the cast.
True/false column bit Accepts 1/0; the strings need converting first.

Identifiers: brackets and case-insensitive collation

SQL Server quotes identifiers with [square brackets], and by default the collation is
case-insensitive, so OrderDate and orderdate are the same column — which
is convenient until two sheets produce headers that differ only in case and get merged into one column.
Column names with spaces need the brackets everywhere, and a header that is a keyword
(order, key, percent) does too; renaming during the mapping step is
still cheaper.

Finding the rows that fail

Bulk loading in SQL Server is fast and unforgiving: BULK INSERT and bcp stop at
the first conversion error unless MAXERRORS is set, and a failed batch inside a transaction rolls
back as a unit. Raising MAXERRORS buys you a partial load — which is useful only if you can
tell which rows arrived.

  • Stage into nvarchar(max) and convert with explicit CAST/TRY_CAST:
    TRY_CAST returns NULL instead of failing, so a query can list the rows it could not read.
  • Keep the sheet row number as a column in staging; it is the only handle back to the spreadsheet.
  • Load in batches so one bad row costs a batch, and check @@ROWCOUNT against the number of
    rows you sent — the cheapest possible reconciliation.

Many sheets, many files, every night

Append every sheet to the same table, or create one table per sheet when the sheets are different entities;
a folder of workbooks runs as a single job. The in-tool session can be saved and scheduled, which is what turns
“the monthly import” into a task rather than a ritual.

Other directions: SQL Server to Excel,
and the same guide for the other two databases —
PostgreSQL
and MySQL.


Posted

in

by