sp_helptext proc_GetCanAddGroup go CREATE proc proc_GetCanAddGroup ( --Returns the permission groups that can be added. For example, after the permission group is added to the current directory, it cannot be added. @CatalogId int ,-- catalog id @GroupId int -- permission id ) as begin with getall as ( select c.*,g.Isinherit,g.e_state from ECM_CatalogTable c left join ECM_Grouping g on c.id=g.c_id where c.[State]=0 ), parent as ( select * from getall g where g.id=@CatalogId union all select g.* from getall g inner join parent p on g.id=p.ParentId ) , getCatalogId as( select id from parent where Isinherit=1 and e_state=0 group by id union select id from parent where e_state= 0 and id=@CatalogId -- union: union, merge ) select * from userrolegroup select groupname as lookname,id as lookvalue from userrolegroup where id not in( select g_id from ECM_Bygroup where r_id in ( select id from ECM_Grouping where c_id in (select id from getCatalogId)) ) and u_state=0 union select groupname as lookname,id as lookvalue from userrolegroup where id in( select g_id from ECM_Bygroup where r_id = @GroupId) and u_state=0 end