create procedure jinhtokc
@jh_no varchar(50)
as
declare @sp_no varchar(50)
declare @ck_no varchar(50)
declare @jh_sl numeric(18,3)
begin
begin tran
select @ck_no=ck_no from jh_zb where jh_no=@jh_no
declare cur_rk cursor for select sp_no,jh_sl from jh_mx where jh_no=@jh_no
open cur_rk
fetch next from cur_rk into @sp_no,@jh_sl
while @@fetch_status=0
begin
select * from kuc where ck_no=@ck_no and sp_no=@sp_no
if @@rowcount > 0
begin
update kuc set kc_sl=kc_sl+@jh_sl where ck_no=@ck_no and sp_no=@sp_no
if @@error > 0 rollback tran
end
else
begin
insert into kuc(ck_no,sp_no,kc_sl) values(@ck_no,@sp_no,@jh_sl)
if @@error > 0 rollback tran
end
fetch next from cur_rk into @sp_no,@jh_sl
end
close cur_rk
deallocate cur_rk
commit tran
end