Tuesday, January 20, 2009

Import Excel and Populate a Uniqueidentifier Column

I have seen several posts around with this problem: When importing data from an Excel sheet, the Import Wizard throws an error saying the uniqueidentifier column in the destination table cannot be populated.

This is acually a very simple problem. If we were importing Excel into our destination table, we would make an identity column and seed the start and incrementation accordingly. Uniqueidentifier columns are GUIDs generated by SQL. They also need to be seeded. In the case of uniqueidenfiers, the seed is (newid).

Here is how I do the import:

1. I create my destination table with the empty column including uniqueidentifier column:

CREATE TABLE [dbo].[Tablename](
[Unique_ID] [uniqueidentifier] NOT NULL DEFAULT (newid()),
[ThingOne] [nvarchar(255)] NULL,
[ThingTwo] [nvarchar(255)] NULL
) ON [PRIMARY]


2. I use the Import Wizard to import my Excel data

3. Be sure to select 'Enable identity insert' on the Mapping section of the Wizard

No comments: