0

This may be a simple query to some of you. But I am not strong in Sql, so expecting some solution for my problem.

I have 2 tables, ProductVenueImport and SupplierVenueImport.

We are dumping all the records from SupplierVenueImport to ProductVenueImport using MERGE clause and a Temp table. Temp will have valid records from SupplerVenuImport and from Temp table we are importing records to ProductVenueImport.

But before importing data to ProductVenueImport from Temp table I need to check for the duplicate records in my target (ProductVenueImport).

For example if I am importing a record with name as 'A', I need to look into ProductVenueImport whether 'A' already existing or not. If it is not existing then only I need to insert 'A' otherwise not.

Could somebody tell me how to do this?

Is using Cursors only the option?

Thanks, Naresh

2
  • Can u provide ur current query so that suggestions can be given accordingly. Also do u simply need to remove duplicate records from ur source table before inserting in target table? If yes i believe it could be done without cursor. Just try searching on net for the same and better try by yourself. Commented Jun 17, 2013 at 5:02
  • @NitinAgrawal, I need to remove the duplicates from 'Temp' table and at the same time I need to backup those records into another 'Temp' table for further usage. Commented Jun 17, 2013 at 5:04

4 Answers 4

1

Assuming the Temp table itself doesn't have duplicates, you could use MERGE like this:

  1. Insert non-existing products.

  2. Do a NO-OP in case of an existing product.

  3. Use $action in the OUTPUT clause to mark which rows were considered for insertion (and inserted) and which for update (but not really updated).

This is what I mean:

DECLARE @noop int;  -- needed for the NO-OP below

MERGE INTO ProductVenueImport AS tgt
USING Temp AS src
ON src.ProductID = tgt.ProdutID
WHEN NOT MATCHED THEN
  INSERT (    column1,     column2, ...)
  VALUES (src.column1, src.column2, ...)
WHEN MATCHED THEN
  UPDATE SET @noop = @noop  -- the NO-OP instead of update
OUTPUT $action, src.column1, src.column2, ...
  INTO anotherTempTable
;
Sign up to request clarification or add additional context in comments.

Comments

0

I think this would do this :

INSERT INTO PRODUCTTBL(FEILD1, FIELD2, FIELD3, FIELD4, FIELD5)
SELECT (FIELD1,FIELD2,FIELD3,FIELD4,FIELD5) FROM TEMP WHERE CRITERIAFIELD NOT IN(SELECT     DISTINCT CRITERIAFIELD FROM PRODUCTTBL)

Comments

0

This should allow you to check for duplicates in a table

select columnname from tablename group by columnname having count(columnname) >1

Comments

0

sorry if I am not getting the question right, can't you use the merge statement on the source table with "When not matched Insert" to insert the new records alone

so in your case it should be like this

merge into ProductVenueImport using temp on (<condition for duplicate>)
when not matched then insert <clause>;

the merge clause will make sure that no duplicate records are inserted into your source table.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.