IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_save_to_ord_users]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_save_to_ord_users]
GO
create procedure [dbo].[mps_save_to_ord_users](
@cmpid int ,
@bizid int , 
@rscid bigint , 
@rsc2id bigint , 
@objids varchar(8000) 
) as
begin
set nocount on
create table #t(dpid int) 
declare @i int,@curInd int,@strlen int,@tmps varchar(100) ,@srcs varchar(8000)
set @srcs = @objids 
select @curInd=1,@strlen=LEN(@srcs) 
while (@curInd<=@strlen) 
begin 
select @i=charindex(',',@srcs,@curInd) 
if (@i>0) 
begin 
select @tmps=SUBSTRING(@srcs,@curInd,@i-@curInd) 
set @curInd=@i+1 
end else if (@curInd<=@strlen) 
begin 
select @tmps=SUBSTRING(@srcs,@curInd,@strlen-@curInd+1) 
set @curInd=@strlen+1 
end 
if (@tmps>'') insert into #t(dpid) values(convert(int,@tmps)) 
end

declare @tbname varchar(50)
declare @dbname varchar(50)
declare @fkey   varchar(50) 
exec sys_sql_getTableNamePk @cmpid,'biz',@bizid,0,@tbname output,@fkey output ,@dbname output 

declare @sql nvarchar(4000) 
declare @rsc2ids varchar(20) 
set @rsc2ids = CONVERT(varchar(20),@rsc2id) 
set @sql = 'delete '+@dbname+@tbname+'_users where ordp_ordid=@rscid and ordp_type like ''_'+@rsc2ids+''' '+CHAR(13)+CHAR(10)+
'insert into '+@dbname+@tbname+'_users(ordp_type,ordp_ordid,ordp_cuid) select ''_'+@rsc2ids+''',@rscid,dpid from #t '+CHAR(13)+CHAR(10) 
exec sp_executesql @sql, N'@rscid bigint ', @rscid 
set nocount off 
end