IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_g_depart_bydpid]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_g_depart_bydpid]
GO
CREATE procedure [dbo].[mps_g_depart_bydpid](
@dpid int 
)
as
begin
set nocount on
declare @t table(id int identity(1,1) , dpid int , level int) 

insert into @t(dpid) 
select dp_id from mps_depart where dp_fdpid=@dpid  and dp_isdepart=0

insert into @t(dpid) 
select dp_id from mps_depart,@t where dp_fdpid=dpid  and dp_isdepart=0

insert into @t(dpid) 
select dp_id from mps_depart,@t where dp_fdpid=dpid  and dp_isdepart=0

insert into @t(dpid) 
select dp_id from mps_depart,@t where dp_fdpid=dpid  and dp_isdepart=0

insert into @t(dpid) 
select dp_id from mps_depart,@t where dp_fdpid=dpid  and dp_isdepart=0

declare @tt table(dpid int) 
insert into @tt(dpid) 
select distinct dpid from @t 

select a.* from mps_depart a , @tt where dp_id=dpid 
order by dp_order , dp_level,dp_code 
set nocount off
end