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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

duplicate data in table

Status
Not open for further replies.

site

Programmer
Oct 23, 2001
44
AT
Hi, All,

When I run this stored procedures more than one times, all data are duplicated in this table? What should I do to solve this problem?

Thanks for your advance.
Jing


The following are my stored procedures:

CREATE PROCEDURE qrycount
@MC_NUM nvarchar(25),
@COMPANY_NAME nvarchar(60),
@NUM_COMPLAINTS nvarchar(20)
AS
begin

--truncate table NUM_OF_COMPLAINTS
insert into NUM_OF_COMPLAINTS
select mc_num, company_name,count(mc_num)as NUM_COMPLAINTS from complaints
where mc_num is not null group by mc_num, company_name
end

 

To prevent duplicates [ol][li]Add a primary key or unique index to the table.
[li]Check if the record already exists before inserting.

select mc_num, company_name, count(mc_num) as NUM_COMPLAINTS from complaints
where mc_num is not null
and not exists (Select * From NUM_OF_COMPLAINTS Where mc_num=complaints.mc_num And company_name=complaints.company_name)
group by mc_num, company_name[/ol] Terry L. Broadbent
faq183-874 contains some tips and ideas for posting questions in these forums. Please review it and comment if you have time.
NOTE: Reference to the FAQ is part of my signature and is not directed at any individual.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top