Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking duplilcate record

Status
Not open for further replies.

siumouse

Programmer
Aug 27, 2003
12
0
0
HK
I'm inserting many records and i need to valid no any duplicate record in table

Then I write a SQL statement as below

if not exists(select * from Poll
where empno = @cEmpno and date = @dDate and
termid = @cTermID and key = @cKeyID)

insert into HrPoll (empno, date, termid, key) values
(@cEmpNo, @dDate, @cTermID, @cKeyID)
GO

Is it a good solution?

Thanks in advance.





 
You mentioned that you need to insert a lot of new records, Where are the values coming from? Are they in a table?
If yes,
A simple select stmt to get the rows that are missing and insert them.
Code:
insert into OldTable 
select   A.EmpNo, A.Date, A.TermId, A.KeyID
from     NewTable A 
         left outer join OldTable B
         on (A.EmpNo = B.EmpNo 
         and A.Date = B.Date 
         and A.TermID = B.TermID 
         and A.KeyID = B.KeyID)
where    B.EmpNo is null

Regards,
AA
 
the value is come from a text file.

i.e. I get a text file and insert into SQL table

Thanks in advance.

 
In that case you can use the code above by replacing the Table names with the appropriate ones.

Regards,
AA
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top