Solved

Import from excel question

Posted on 2014-01-30
6
193 Views
Last Modified: 2014-02-11
I am using an excel spreadsheet to import some data into a table in our sql database.  The issue is that the table contains a field that is a unique number that is generated for each contact address.  It is not used anywhere else in the database, but it is not nullable.  

I am using the import wizard to import this data - Is there a command I can add to the query it generates that will increment this number for every new entry the SQL import tries to insert?
0
Comment
Question by:mjburgard
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 35

Expert Comment

by:David Todd
ID: 39822034
Hi,

Is this number in the spreadsheet?

Is this number an identity column in SQL?

As a work around, import the spreadsheet into another table, and then copy the contents from this work table to the main one.

ie
insert dbo.MainTable( col2, col3, col4 )
select w.col2, w.col3, w.col4
from dbo.WorkTable w
left outer join dbo.MainTable m
    on m.SomeKeyCol = w.SomeKeyCol
where
    m.SomeKeyCol is null
;

HTH
  David
0
 
LVL 1

Author Comment

by:mjburgard
ID: 39822267
I will try the work around -
It is a number, however it is not a key, but it does appear in the Indexes folder of the table.
0
 
LVL 35

Expert Comment

by:David Todd
ID: 39822677
Hi,

I didn't ask if this was a key.

I asked if it was an identity column.

Regards
  David
Capture.PNG
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 1

Author Comment

by:mjburgard
ID: 39822766
Yes -
it is an identity column
0
 
LVL 35

Accepted Solution

by:
David Todd earned 500 total points
ID: 39822890
Hi,

Because it is an identity column, you can't (normally) specify a value for that.

So if the destination table looks like this
Create table dbo.MyTable(
    MyTableID int identity( 1, 1 )
    , SomeKey int
    , SomeOtherColumns varchar( 200 )
)

Then your insert needs to look something like this
insert dbo.MyTable( SomeKey, SomeOtherColumns )
select
    w.SomeKey
    , w.SomeColumns
from dbo.WorkTable w
left outer join dbo.MyTable m
    on m.SomeKey = w.SomeKey
where
    m.SomeKey is null
;

HTH
  David
0
 
LVL 1

Author Closing Comment

by:mjburgard
ID: 39850245
This was what I needed to get the job finished.  There was a bit more that I needed to do, but this solved the issue I was having.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question