IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_ding_addapps]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_ding_addapps]
GO
create PROCEDURE [dbo].[mps_ding_addapps]  
 @useruid int,  
 @conditions nvarchar(max)  
AS  
BEGIN  
 SET NOCOUNT ON;  
 declare @index int =0   
 declare @item varchar(100)   
 declare @item2 varchar(100)   
 declare @inputData table(id int,biztype int)  
 declare @tempDing table(dm_bizid int,dm_cuid int,dm_type int,dm_category nvarchar(50),dm_title nvarchar(50),dm_icon nvarchar(80),dm_subcategory nvarchar(50))  
 /*处理字符串，插入@inputData*/  
 while @conditions> ''  
 begin  
  set @index=CHARINDEX('|',@conditions);  
  if(@index>0)  
  begin  
   set @item = SUBSTRING(@conditions,0,@index);  
   set @conditions = SUBSTRING(@conditions,@index+1,len(@conditions));  
  end else begin  
    set @item=@conditions   
    set @conditions=''  
  end  
  if(@item>'')  
  begin  
   set @index = CHARINDEX(',',@item)  
   set @item2 = SUBSTRING(@item,@index+1,len(@item));  
   set @item = SUBSTRING(@item,0,@index);  
   insert into @inputData(id,biztype) values(CONVERT(int,@item),CONVERT(int,@item2))  
  end  
 end  
 /*确认权限插入@tempDing*/  
 insert into @tempDing  
 select distinct(biz_id),@useruid,1,biz_category,biz_title,biz_icon,biz_subcategory   
 from mps_biztype,mps_biztype_users,@inputData   
 where id = biz_id and id = bp_bizid and bp_cuid =@useruid and bp_type in ('biznew','bizview') and biztype = 1  
 insert into @tempDing  
 select distinct(rpt_id),@useruid,2,rpt_category,rpt_title,rpt_icon,rpt_categorysub   
 from mps_report,mps_report_users,@inputData   
 where id = rpt_id and id = rptp_rptid and rptp_cuid = @useruid and rptp_type in ('rptnew','rptview') and biztype = 2  
 
  insert into @tempDing  
 select distinct(pt_id),@useruid,3,'',pt_caption,pt_icon0,''   
 from mps_programtype,mps_programtype_users,@inputData   
 where id = pt_id  and pp_type in ('prgview') and biztype = 3
 --and id = pp_ptid and pp_cuid = @useruid
 
 /*对比@tempDing和mps_ding_myapp避免插入重复数据*/  
 /*删除mps_ding_myapp表和@tempDing表中都存在的*/  
 delete   a
 from @tempDing a ,mps_ding_myapp  b
  where b.dm_cuid = @useruid and a.dm_bizid = b.dm_bizid and a.dm_type = b.dm_type
 /*插入到表mps_ding_myapp*/  
 insert into mps_ding_myapp(dm_bizid,dm_cuid,dm_type,dm_category,dm_title,dm_icon,dm_subcategory)  
 select* from @tempDing  
 set nocount off;  
END