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

delete @t 

set @s = @dtsids 
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 mps_depart_title_sub from @t where dts_id=id 
end