IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_g_depart_bydpid_sql]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_g_depart_bydpid_sql]
GO
CREATE procedure [dbo].[mps_g_depart_bydpid_sql](
@dpid int ,
@bizsid int,
@extparam1 nvarchar(50),
@extparam2 nvarchar(50),
@extparam3 nvarchar(50),
@extparam4 nvarchar(50),
@extparam5 nvarchar(50),
@extparam6 nvarchar(50),
@extparam7 nvarchar(50),
@extparam8 nvarchar(50),
@extparam9 nvarchar(50) 
)
as
begin
set nocount on
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
create table #t(id int identity(1,1) , dpid int , level int) 

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

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

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

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

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

create table #tt(dpid int) 
insert into #tt(dpid) 
select distinct dpid from #t

declare @sql1 nvarchar(4000) 
declare @sql nvarchar(max) 
select @sql1=bizs_condition from  mps_biztype_source with(nolock)  where bizs_id=@bizsid 
set @sql1 = ISNULL(@sql1,'') 
if (@sql1>'') set @sql1 = ' and ('+@sql1+') '

set @sql = '
select a.* from mps_depart a with(nolock)  , #tt where dp_id=dpid '+@sql1+'
order by dp_order , dp_level,dp_code '

exec sp_executesql @sql,
N'
@extparam1 nvarchar(50),
@extparam2 nvarchar(50),
@extparam3 nvarchar(50),
@extparam4 nvarchar(50),
@extparam5 nvarchar(50),
@extparam6 nvarchar(50),
@extparam7 nvarchar(50),
@extparam8 nvarchar(50),
@extparam9 nvarchar(50)',
@extparam1 ,
@extparam2 ,
@extparam3 ,
@extparam4 ,
@extparam5 ,
@extparam6 ,
@extparam7 ,
@extparam8 ,
@extparam9

drop table #t 
drop table #tt 

set nocount off
end