Solved

Get rid of Duplicate records in MS SQL 2000

Posted on 2006-11-29
2
426 Views
Last Modified: 2011-09-20
Table R
Category   Classes  Brand      Model      
AAA          BBB       CCC        DDD
BBB          CCC       DDD        EEE
CCC         AAA       BBB         ZZZ
CCC         AAA       BBB         EEE

Table A
Category   Classes  Brand      Model      
AAA          BBB       CCC        AAA
BBB          CCC       DDD        FFF
CCC         AAA       BBB         ZZZ (* - already exist in table R)
CCC         AAA       CCC        ZZZ

I need to insert data in Table A into Table B, but don't want the duplicate record (see *) as Table R is a reference table and no duplicate records in allowed.  How to prevent Table R from having duplicate records after the insertion?

Please advice.

Thank you.
0
Comment
Question by:woo_kh
2 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 50 total points
ID: 18043360
INSERT INTO R
SELECT *
FROM A a1
WHERE NOT EXISTS (SELECT 1 FROM R WHERE Category = A1.Category and Classes = A1.Classes and Brand = A1.Brand AND Model = A1.Model)
0
 
LVL 17

Expert Comment

by:HuyBD
ID: 18043373
insert into tableA
select * from tableB B
left join tableR R on R.Category=B.Category and R.Brand=B.Brand and R.Model=B.Model
where R.Category is null
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now