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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.