IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_depart_title_insert]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_depart_title_insert]
GO
create procedure [dbo].[mps_depart_title_insert] 
@dpid int ,
@otids varchar(200) 
as
begin
declare @s varchar(200) 
declare @s1 varchar(100)
declare @i int 
declare @t table(id int) 
set @s = @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 
if (@s1>'') 
insert into @t(id) values(convert(int,@s1))
end 
delete @t from mps_depart_title where dt_dpid=@dpid and dt_otid=id 
insert into mps_depart_title(dt_dpid,dt_otid) select @dpid , id from @t 
insert into mps_depart_title(dt_dpid,dt_otid) select dp_id , id from @t,mps_depart where dp_fdpid = @dpid and dp_isdepart =1
end