Sql server中cursor的使用



ALTER PROCEDURE [dbo].[usp_cm_mapping_reference_id_commonplace]
AS
SET NOCOUNT ON;
BEGIN
 declare @master_location_id int;
 declare @commonplace_name varchar(300);

 declare my_cursor cursor for
 select master_location_id,common_place_name
 from master_location_common_place group by master_location_id,common_place_name
 having count(*)>1

 open my_cursor
 fetch next from my_cursor into @master_location_id,@commonplace_name
 while @@FETCH_STATUS= 0
 Begin
  --logic
  delete from master_location_common_place where
master_location_id=@master_location_id and common_place_name=@commonplace_name
  and master_location_common_place_id not in
   (select min(master_location_common_place_id) from master_location_common_place
   where
master_location_id=@master_location_id and common_place_name=@commonplace_name)

  --Get id of insert
  set @new_master_location_id=SCOPE_IDENTITY();
  
  --reference_id
  select @reference=reference_id from master_location where
master_location_id=@master_location_id;
  if(isnull(@reference,'')='')
  Begin 
   update master_location set
reference_id=@reference_id where master_location_id=@master_location_id;
  End
  else
  Begin
   if(isnull(@reference_id,'')!='')
   Begin
    if(charIndex(cast(@reference_id as varchar),@reference)=0)
    Begin
     set @reference=@reference+','+cast(@reference_id as varchar);
     update master_location set
reference_id=@reference where master_location_id=@master_location_id;
    End
   End
  End

  fetch next from my_cursor into @master_location_id,@commonplace_name
 End
 close my_cursor
 deallocate my_cursor
END

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值