IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[order_213_calc_relation]') AND type = 'P')
DROP PROCEDURE [dbo].[order_213_calc_relation]
GO
CREATE  procedure order_213_calc_relation(@cmpid int,@ordid bigint) as begindeclare @t table(bizid int , ordid bigint) insert into @t(bizid,ordid) values(213,@ordid) insert into @t(bizid,ordid) select ord_fbizid , ord_fordid from order_213where ord_id=@ordid and ord_ftype='biz' and ord_ford2tag=0 and ord_fordid>0 declare @ic int select @ic = count(*) from @t if (@ic<=1) return   declare @ic2 int while (@ic=@ic)beginselect @ic2=count(1) from @t if (@ic2=@ic) break  -- 如何平衡，则退出。set @ic=@ic2 end  declare @mybizid int declare @myordid bigintdeclare status1 cursor LOCAL FORWARD_ONLY READ_ONLY for select bizid,ordid  from @t  open status1FETCH NEXT FROM status1 INTO @mybizid,@myordidWHILE (@@FETCH_STATUS = 0)BEGINif (@mybizid=213) begininsert into order_213_relation(ordr_ordid,ordr_rcrmid,ordr_rordid) select @myordid , bizid , ordid from @t where bizid<>@mybizid and ordid<>@myordid and (not exists (select 1 from order_213_relation where ordr_ordid=@myordid and ordr_rcrmid=bizid and ordr_rordid=ordid)) end FETCH NEXT FROM status1 INTO @mybizid,@myordid ENDCLOSE status1DEALLOCATE status1 end