Showing posts with label keys. Show all posts
Showing posts with label keys. Show all posts

Sunday, March 25, 2012

bulk insert which check if record exists

Hi All,
I want to bulk insert from table A to table B (both table have the same
design fields which contain primary keys). Is there a way to check if record
exist before inserting rows to avoid primary key violation? What would be my
query?
thanks in advance,
joelTry something like this:
insert tableA (<cols> )
select b.<cols>
from tableA b
left outer join tableA a
on b.PrimaryKey = a.PrimaryKey
where a.PrimaryKey IS NULL|||If you assume that ID is your PK then you can do something like this
INSERT INTO TABLEB
SELECT A.*
FROM TABLEA A
LEFT JOIN TABLEB B
ON A.ID = B.ID
WHERE B.ID IS NULL
http://sqlservercode.blogspot.com/|||Sorry, tableA and tableB are backwards in my example. This will insert
into tableA, rows that are in tableB and not in tableA.
insert tableB (<cols> )
select a.<cols>
from tableA a
left outer join tableB b
on b.PrimaryKey = a.PrimaryKey
where b.PrimaryKey IS NULL|||Sorry again, untested code bites again.
This will insert
into tableB, rows that are in tableA and not in tableB.
insert tableB (<cols> )
select a.<cols>
from tableA a
left outer join tableB b
on b.PrimaryKey = a.PrimaryKey
where b.PrimaryKey IS NULLsql

Saturday, February 25, 2012

Bulk insert

Hi,
I want to use bulk insert using a text file to import data from it to the DB, but how can I ignore the primary keys in the text file which is in the db table.
regards,I think there is no direct way to deal with to ignore PK constraint while Bulk inserting. You need to drop them before insert and re-enable once it finishes.|||Originally posted by Satya
I think there is no direct way to deal with to ignore PK constraint while Bulk inserting. You need to drop them before insert and re-enable once it finishes.

Which will fail if this is an issue for you now...

Why not load the data to a staging table, and audit the data before dropping it in to the destination table?|||I know other solutions but I want this one because the system is a real time system and I want the fastest way which is bulk insert.

regards,

Originally posted by Brett Kaiser
Which will fail if this is an issue for you now...

Why not load the data to a staging table, and audit the data before dropping it in to the destination table?|||If you want to "skip" a field, it's in BOL under "Using a Data File with Fewer Fields"...Wait a minute, I found this in my clipboard...Am I loosing my mind, or it's the same post? Oh, I get it, Groundhog day all over...hehehehe, I know all the answers!!!!!!!!