IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_g_branch_bybcid_sql]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_g_branch_bybcid_sql]
GO
create procedure [dbo].[mps_g_branch_bybcid_sql](
@bcid 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
declare @sql1 nvarchar(4000) 
declare @sql nvarchar(max) 
select @sql1=bizs_condition from  mps_biztype_source where bizs_id=@bizsid 
set @sql1 = ISNULL(@sql1,'') 
if (@sql1>'') set @sql1 = ' and ('+@sql1+') '

create table #t(id int identity(1,1) , bcid int , level int) 
insert into #t(bcid,level) 
select bc_id , bc_level from mps_branch order by bc_order , bc_level ,bc_code 
declare @id int 
select @id=id from #t where bcid=@bcid 
delete #t where id<=@id 
set @id=0
select top 1 @id=id from #t where level=0 order by id 
set @id = isnull(@id,0) 
if (@id>0) delete #t where id>=@id 
set @sql = '
select a.* from mps_branch a , #t where bcid=bc_id  '+@sql1+' order by bc_order , bc_level,bc_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 
set nocount off
end