IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_delete_depart]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_delete_depart]
GO
create procedure [dbo].[mps_delete_depart]
@cmpid int,
@mydpid int,
@branchid int,
@userdpid int,
@useruid int,
@userotid int
AS
BEGIN
 SET NOCOUNT ON

declare @operation varchar(50)
declare @errorinfo nvarchar(4000) 
declare @result1 varchar(100)
-- Insert statements for procedure here
declare @empcount bigint
set @empcount = 0;
select @empcount = count(*) from mps_depart, mps_employee
where dp_id = cu_dpid and dp_id = @mydpid
if (dbo.mps_sys_checkkeywords('dpt.beforecreate,dpt.aftercreate',@cmpid,0,'','')=0)   -- 有事件
begin
exec drop_temp_table '#old'
select * into #old from mps_depart where dp_id = @mydpid
end
--部门删除之前
if (dbo.mps_sys_checkkeywords('dpt.beforedelete',@cmpid,0,'','')=0) 
begin
exec mps_depart_delete_before @cmpid,@branchid,@userdpid,@useruid,@userotid,@mydpid,@operation output,@errorinfo output
if @operation = 'abort' 
begin
select @operation operation,@errorinfo errorinfo
return 
end
end

if @empcount = 0
begin
declare @basedpid bigint 
set @basedpid  = 0
select @basedpid = dp_dpstdid from mps_depart where dp_id = @mydpid
if not exists(select * from
mps_login_role ro with(nolock),
mps_depart dp with(nolock)
where  dp.dp_id = ro.lgu_dpid
AND dp.dp_id = @mydpid
)
begin
DELETE mps_depart WHERE dp_id = @mydpid
delete mps_depart_base where dp_id = @basedpid and dp_isgroup = 1
set @result1 = 'ok'
end else 
begin
select 'abort' operation,'此部门下还存在角色' errorinfo
return
end
end
else set @result1 = @empcount

select @result1 result1
--部门删除之后
if (dbo.mps_sys_checkkeywords('dpt.afterdelete',@cmpid,0,'','')=0) 
begin
exec mps_depart_delete_after @cmpid,@branchid,@userdpid,@useruid,@userotid,@mydpid
end

 SET NOCOUNT Off
END