使用while循环和伪列的存储过程如下:
USE [JointFrame2] GO /****** Object: StoredProcedure [dbo].[Proc_enterprise_unified_sam_while] Script Date: 2017/04/06 9:39:19 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO --此存储过程将污染源名称相似度大于80%的数据的主子记录标识设置为初始值(while循环方式) create PROCEDURE [dbo].[Proc_enterprise_unified_sam_while] AS --=============================================================================== BEGIN DECLARE @id VARCHAR(40), --记录ID @psname VARCHAR(220), ---污染源名称 @region_code VARCHAR(20), --污染源区域编码 @i BIGINT, --记录主记录总数 @j BIGINT, --记录内层循环的执行 @k BIGINT --记录外层循环的执行 set @k = 1 -- --创建临时表#tb1,用于存放非 -- create table #tab1( --id varchar(50), --region_code varchar(20), --psname varchar(220) -- ) --创建临时表#tb2用于存储相似度大于80%的数据 create table #tab2( id varchar(50) ) select RowNum=IDENTITY(INT,1,1),id,region_code,psname into #tab1 from t_unified_enterprise_info where main_or_child = 1 and system_source != 0 select @i = max(RowNum) from #tab1 while(@i>@k) BEGIN --查找当前条数据,将其ID赋值给@id,将region_code赋值给@region_code,将psname赋值给@psanme select @id = id,@region_code = region_code,@psname = psname from #tab1 where RowNum = @k --将相似度超过80%的数据存放到临时表#tb2中 insert into #tab2 select id from ( select *,( CASE when u.a1=1 then 1 when u.b1=1 then 1 else 0 end) as c1 from ( SELECT id,psname,dbo.FN_Resemble(@psname,psname) as a1,dbo.FN_Resemble(psname,@psname) as b1 FROM [dbo].[t_unified_enterprise_info] where region_code=@region_code)u where u.a1>=0.6) uu where (uu.a1+uu.b1)/2>0.8 select @j = count(*) from #tab2 if(@j>1) BEGIN print @k -- update t_unified_enterprise_info set main_or_child = 0,parentid = NULL,unique_code = NULL where id in (select id from #tab2) END set @k = @k + 1 delete from #tab2 END delete from #tab1 END --drop table #tab1 --drop table #tab2 --exec [Proc_enterprise_unified_sam_while]