Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 inserting data

Author  Topic 

jamiesw
Starting Member

6 Posts

Posted - 2014-06-30 : 11:18:23
Hi there,
I have a table(MyTable)that also has an unique index that contains 3 fields in the index.
something like:
ID(PK)|FName|LName|DOB|PostCode
Lname, Dob & Postcode are all part of the unique index.

I also have a table variable in my stored proc that has the same fields plus a few more.
I am trying to do an insert from my @tablevariable into my table.

Insert INTO MyTable(Values)
Select Values from @tablevariable

I get the "Cannot insert duplicate key row in object" error,
I need the insert to continue with inserting the rest of the records that don't match regardless.

Or be able to delete matching values that already exist from @tablevariable
like loop through the table and delete if found from @tablevarible

not sure if explaining my self properly,
Thanks
Jamie

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2014-06-30 : 12:43:56
Insert INTO MyTable(Values)
Select Values from @tablevariable tv where not exists (select * from MyTable mt where tv.Lname = mt.Lname and tv.Dob = mt.Dob and tv.Postcode = mt.Postcode)

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

jamiesw
Starting Member

6 Posts

Posted - 2014-06-30 : 13:39:28
Thanks exactly what I was looking for. My head went blank and was staring at it for ages.

Thanks Again
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2014-06-30 : 13:40:28


Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page
   

- Advertisement -