IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_depart_title_sub_insert]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_depart_title_sub_insert]
GO
CREATE procedure [dbo].[mps_depart_title_sub_insert] 
@dpid int,
@otid int,
@dpids varchar(300),
@otids varchar(300) 
as
begin
declare @s varchar(300)
declare @ss varchar(300)
declare @s1 varchar(100) 
declare @s2 varchar(100) 
declare @i int 
declare @t table(dpid int , otid int) 
set @s=@dpids 
set @ss=@otids 
while (@s>'') 
begin
set @i = charindex(',',@s) 
if (@i>0) 
begin
set @s1 = substring(@s,1,@i-1) 
set @s = substring(@s,@i+1,len(@s)-@i) 
end else 
begin
set @s1 = @s 
set @s='' 
end 
set @i = charindex(',',@ss) 
if (@i>0) 
begin
set @s2 = substring(@ss,1,@i-1) 
set @ss = substring(@ss,@i+1,len(@ss)-@i) 
end else 
begin
set @s2 = @ss 
set @ss='' 
end 
if (@s1>'' and @s2>'') 
insert into @t(dpid,otid) values(convert(int,@s1),convert(int,@s2)) 
end 
delete @t where dpid=@dpid  
delete @t from mps_depart_title_sub 
where dts_dpid=@dpid and dts_otid=@otid and dts_dpids=dpid and dts_otids=otid 
insert into mps_depart_title_sub(dts_dpid,dts_otid,dts_dpids,dts_otids)
select @dpid , @otid , dpid , otid 
from @t 
end