Create Primary Key after Bulk Insert


I am building a bulk insert query in SQL 2012.  

Everything I want has been accomplished except for the item below:

When I run the bulk insert I need a way to

1.) create a primary key based on 2 of the columns in the flat file (I have attached this in an excel file as an example of how it looks).

2.) If I run the bulk insert query again I want the rows to fail that have the same primary key.  For example, if records have been imported with the DTAL_RRN = 48232604 and EXTERNAL_SN = 2085 already I would want these records to fail on import if someone tries to run the query again.  I used this example because it is the first line in the attached file.

The Bulk insert looks like this:

bulk insert DTAL_ALL




            FIRSTROW = 2,


I also was having trouble removing quotes (") using the FieldTerminator so I wrote another query to remove the quotes from the fields with the replace function:



 DTAL_RRN=replace (DTAL_RRN ,'"',' ')

I don't know if I am being to specific here - all I really need is to create that primary key mentioned above and once created have it reject rows with the same primary key when another bulk insert is run.
Michael GrahamData AnalystAsked:
Who is Participating?
Scott PletcherSenior DBACommented:
Typically a staging table is used for this, and I strongly recommend you do this for your task as well.  That is, a separate, "work" table, not the main table, is loaded (imported) with the potential new rows.  You use that work table to do editing for "s, to check for and remove dups, etc..  When the data is clean, you insert to the main table.
Mark WillsTopic AdvisorCommented:
Absolutely agree with Scott...

And that way, you create your 'real' table with all the indexes you need.

Would hope that it already has primary keys, but if not you can create
ALTER TABLE  your_table   
ADD CONSTRAINT PK_meaningful_name PRIMARY KEY CLUSTERED (column1,column2);  

Open in new window

Columns cannot be NULL so you might want to alter table yourtable alter column columnname datatypedefinition NOT NULL
Michael GrahamData AnalystAuthor Commented:
Thanks guys, awesome advice!
Michael GrahamData AnalystAuthor Commented:
Thanks again guys.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.