查找一个表中重复记录的存储过程(因为数据是从Excele中导到表中的, 表还没有建立主Key)
CREATE proc sp_check_data
as
begin
declare @i int
declare @var_vkorg varchar(10) ,@vkbur varchar(10) , @var_vtweg varchar(10) ,@var_kunnr varchar(10) ,
@var_kunwe varchar(10) ,@fee_code varchar(10) ,@exp_code varchar(10) ,@pay_type varchar(10)
declare @var_vkorg1 varchar(10) ,@vkbur1 varchar(10) , @var_vtweg1 varchar(10) ,@var_kunnr1 varchar(10) ,
@var_kunwe1 varchar(10) ,@fee_code1 varchar(10) ,@exp_code1 varchar(10) ,@pay_type1 varchar(10)
set @i = 0
set @var_vkorg1 =''
set @vkbur1 =''
set @var_vtweg1 =''
set @var_kunnr1 =''
set @var_kunwe1 =''
set @fee_code1 =''
set @exp_code1 =''
set @pay_type1 =''
Declare Salesman CURSOR
for
SELECT VKORG, VKBUR, VTWEG, KUNNR, kunwe, fee_code, exp_code, pay_type
FROM TM_Expense_Rebate_Rate
order by VKORG, VKBUR, VTWEG, KUNNR, kunwe, fee_code, exp_code, pay_type
OPEN Salesman
FETCH Salesman INTO @var_vkorg,@vkbur, @var_vtweg,@var_kunnr, @var_kunwe,@fee_code,@exp_code,@pay_type
WHILE @@fetch_status = 0
begin
if @var_vkorg = @var_vkorg1 and @vkbur=@vkbur1 and @var_vtweg=@var_vtweg1 and @var_kunnr =@var_kunnr1 and
@var_kunwe = @var_kunwe1 and @fee_code =@fee_code1 and @exp_code=@exp_code1 and @pay_type =@pay_type1
begin
set @i =@i + 1
set @var_vkorg1 = @var_vkorg
set @vkbur1 = @vkbur
set @var_vtweg1 = @var_vtweg
set @var_kunnr1 = @var_kunnr
set @var_kunwe1 = @var_kunwe
set @fee_code1 =@fee_code
set @exp_code1 = @exp_code
set @pay_type1 = @pay_type
update TM_Expense_Rebate_Rate
set flag = '1'
where VKORG =@var_vkorg
and VKBUR =@vkbur
and VTWEG =@var_vtweg
and KUNNR = @var_kunnr
and kunwe = @var_kunwe
and fee_code =@fee_code
and exp_code = @exp_code
and pay_type = @pay_type
end
else
begin
set @i = 1
set @var_vkorg1 = @var_vkorg
set @vkbur1 = @vkbur
set @var_vtweg1 = @var_vtweg
set @var_kunnr1 = @var_kunnr
set @var_kunwe1 = @var_kunwe
set @fee_code1 =@fee_code
set @exp_code1 = @exp_code
set @pay_type1 = @pay_type
end
FETCH Salesman INTO @var_vkorg,@vkbur, @var_vtweg,@var_kunnr, @var_kunwe,@fee_code,@exp_code,@pay_type
end
CLOSE Salesman
DEALLOCATE Salesman
if @@error<>0
begin
rollback tran
end
end
GO