IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_save_account_depart]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_save_account_depart]
GO
create procedure [dbo].[mps_save_account_depart](
@mybcid int ,
@mydpids varchar(1000),
@myaiid int)
as 
begin
set nocount on
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
declare @t table(dpid int) 
declare @s1 varchar(1000)
declare @s2 varchar(100)
declare @i int 
set @s1 = @mydpids 
while (@s1>'') 
begin
set @i = charindex(',',@s1)
if (@i>0)
begin
set @s2 = substring(@s1,1,@i-1)
set @s1 = substring(@s1,@i+1,len(@s1)-@i)
end else
begin
set @s2=@s1
set @s1=''
end
if (@s2>'')
begin
insert into @t(dpid) values(convert(int,@s2)) 
end
end 
delete @t where (exists (select 1 from mps_account_depart where aid_bcid=@mybcid and aid_aiid=@myaiid and aid_dpid=dpid)) 
insert into mps_account_depart(aid_bcid,aid_dpid,aid_aiid) 
select @mybcid , dpid,@myaiid from @t 
set nocount off 
end