IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_g_branch_bybcid]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_g_branch_bybcid]
GO
CREATE procedure [dbo].[mps_g_branch_bybcid](
@bcid int 
)
as
begin
set nocount on
declare @t table(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 
select a.* from mps_branch a , @t where bcid=bc_id order by bc_order , bc_level,bc_code 
set nocount off
end