sql - How to tell where the stored procedure creates duplicates -


the sp used add additional unit existing customer. there 3 tables used. information entered first table, partially info first table entered second table, , last table has info first table once again. problem every time add new unit unit before (ub) ends duplicated 8 times. if add unit 8 rows added unit (ub). moreover, when duplicates created new id's assigned pks. suggestions on how fix problem.

thank you.

alter procedure [dbo].[addadditionalunit] (        @fpn varchar(20),      @un varchar(20),     @effd datetime,      @expd datetime,                  @endersnr int,     @prm decimal(19,2),     @cid int,     @covptid int,        @addid varchar(7),       @modid varchar(7),     @custid int )  begin      set nocount on;  insert policys (     facility_policy_number,      unit_number,      effective_date,      expiration_date,      endorsement_date,     endorsement_nbr,     premium,     coverage_id,     coverage_percent_theft_id,     add_id,     add_date,     mod_id,     mod_date  ) values (        @fpn,      @un,     @effd,     @expd,       getdate(),     @endersnr,     @prm,     @cid,     @covptid,     @addid,     getdate(),     @modid,     getdate() ) insert policy_facility_customer (     cert_number,      facility_id,      customer_id,      add_id,      add_date,      mod_id,      mod_date )  select          policys.cert_number,      facilitys.id facilityid,      @custid customer_id,      policys.add_id,      policys.add_date,      policys.mod_id,      policys.mod_date          facilitys inner join                       policys on facilitys.facility_policy_number = policys.facility_policy_number      (facilitys.facility_policy_number = @fpn)  declare @ptcid int set @ptcid = 1;   insert policys_transactions (             cert_number,       facility_policy_number,       unit_number,       effective_date,       expiration_date,       endorsement_date,       endorsement_nbr,       premium,       coverage_id,       coverage_percent_theft_id,       policy_tran_code_id,       add_id,       add_date,       mod_id,       mod_date  )  select    policys.cert_number,      policys.facility_policy_number,      policys.unit_number,     policys.effective_date,     policys.expiration_date,     policys.endorsement_date,     policys.endorsement_nbr,     policys.premium,     policys.coverage_id,     policys.coverage_percent_theft_id,     @ptcid,      policys.add_id,      policys.add_date,      policys.mod_id,      policys.mod_date          facilitys inner join                       policys on facilitys.facility_policy_number = policys.facility_policy_number      (facilitys.facility_policy_number = @fpn)  end 

ok here happening, want insert 1 record , 1 record second table based on inserted first table not specifying in clause record except through not primary key , not unique.

you should return pk of record inserted in first table using output clause (sql server specific, there may differnt method differnt database backend) table variable.

then should join first table table vairaible 1 record want. further, best practice never insert table without using not exists clause ensure not duplicating exisiting data.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -