[sqlserver] 游标循环示例

DECLARE @C_find_contract_person_name varchar(1000)
DECLARE contract_CURSOR CURSOR FOR (
	SELECT ent_id,contract_person_name from ent_contract_person 
	group by ent_id,contract_person_name having count(1)>1
)
open  contract_CURSOR 
	fetch next from contract_CURSOR into @C_find_ent_id,@C_find_contract_person_name
while  (@@fetch_status=0)
	begin
		DECLARE @con_sum int
		DECLARE @con int
    DECLARE @del_id varchar(8000)
		DECLARE @contract_person_id int
		DECLARE @ent_id int 
		DECLARE @contract_person_name varchar(40)
		DECLARE @contract_person_email varchar(100)
		DECLARE @all_email varchar(8000)
		DECLARE @all_sms varchar(8000)
		DECLARE @contract_person_sms varchar(30)
		DECLARE update_CURSOR CURSOR FOR (
			select a.*,b.con_sum from (
				SELECT contract_person_id,ent_id,contract_person_name,contract_person_email,contract_person_sms from ent_contract_person
				where ent_id=@C_find_ent_id and contract_person_name=@C_find_contract_person_name
			)a left join (
				SELECT ent_id,contract_person_name,count(1) con_sum from ent_contract_person
				where ent_id=@C_find_ent_id and contract_person_name=@C_find_contract_person_name
				group by ent_id,contract_person_name
			) b on a.ent_id=b.ent_id and a.contract_person_name=b.contract_person_name
		)
		open  update_CURSOR 
				fetch next from update_CURSOR into @contract_person_id,@ent_id,@contract_person_name,@contract_person_email,@contract_person_sms,@con_sum
		set @con=1
		set @all_email=''
		set @all_sms=''
    set @del_id=''
		while  (@@fetch_status=0)
			begin
				if (LEN(@contract_person_email)>0)
					begin
            if CHARINDEX(@contract_person_email,@all_email)=0
              begin
                set @all_email=@all_email+(case when len(@all_email)>0 then ';' else '' end)+@contract_person_email
              end
					end
				if LEN(@contract_person_sms)>0
					begin
            if CHARINDEX(@contract_person_sms,@all_sms)=0
              begin
                set @all_sms=@all_sms+(case when len(@all_sms)>0 then ';' else '' end)+@contract_person_sms
              end
					end
        
        if @con<>@con_sum
					begin
            set @del_id=@del_id+(case when len(@del_id)>0 then ',' else '' end)+cast(@contract_person_id as varchar(100))
					end

				if @con=@con_sum
					begin
						--print cast(@con as varchar(100))+'●◆▼★▲▼█'+@all_email+'●◆▼★▲▼█'+@all_sms
            --print cast(@con as varchar(100))+'●◆▼★▲▼█'+@del_id
            exec('delete from ent_contract_person where contract_person_id in ('+@del_id+')')
            update ent_contract_person set contract_person_email=@all_email,contract_person_sms=@all_sms where contract_person_id=@contract_person_id
					end
				set @con=@con+1
				fetch next from update_CURSOR into @contract_person_id,@ent_id,@contract_person_name,@contract_person_email,@contract_person_sms,@con_sum
			end
		close update_CURSOR
		deallocate update_CURSOR

	fetch next from contract_CURSOR into @C_find_ent_id,@C_find_contract_person_name
	end
close contract_CURSOR
deallocate contract_CURSOR



你可能感兴趣的:([sqlserver] 游标循环示例)