IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_newdepart]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_newdepart]
GO
CREATE procedure [dbo].[mps_newdepart] 
@cmpid int ,
@branchid int,
@dp_code varchar(20) ,
@dp_depart nvarchar(50) as
begin
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
declare @dpid int 
declare @stddpid int 
select top 1 @dpid = dp_id from mps_depart where dp_cmpid=@cmpid and dp_code=@dp_code and dp_bcid=@branchid 
set @dpid = isnull(@dpid , 0 ) 
if (@dpid=0) 
begin
insert into mps_depart_base(dp_cmpid , dp_code , dp_depart , dp_isgroup) 
values(@cmpid  , @dp_code , @dp_depart , 0) 
select top 1 @stddpid = dp_id from mps_depart_base where dp_cmpid=@cmpid and dp_code=@dp_code and dp_isgroup=0  
set @stddpid = isnull(@stddpid , 0 ) 
insert into mps_depart(dp_cmpid ,dp_bcid, dp_code , dp_depart,dp_dpstdid) 
values(@cmpid  , @branchid, @dp_code , @dp_depart,@stddpid) 
select top 1 @dpid = dp_id from mps_depart  where dp_cmpid=@cmpid and dp_code=@dp_code and dp_bcid=@branchid 
select * from mps_depart  where dp_id=@dpid 
-- 如果是集团，则要自动在基表中增加记录
end else 
begin
select 0 none where 0=1 
end 
end