IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[ORDER_40_ADD_TITLE_2_DEPART]') AND type = 'P')
DROP PROCEDURE [dbo].[ORDER_40_ADD_TITLE_2_DEPART]
GO

--已知部门存在加岗位
CREATE PROCEDURE ORDER_40_ADD_TITLE_2_DEPART(
@branchname varchar(100) = '', --'重庆仟丝缘商业连锁有限公司',
@depart varchar(1000) = '' ,--'结算组',
@title varchar(100) ,
@otid bigint  output 
)
AS
BEGIN

--select *  from mps_groupbranchwhere bc_cmpid = 1 
--select * from mps_title order by ot_id
--SELECT * FROM mps_title
declare 
@bcid int ,
@dpid int,
@dtid int ,
@order int = 99999,
@displayLevel varchar(100) ='&emsp;&emsp;&emsp;&emsp;&emsp;'

select @bcid = bc_id  from mps_groupbranchwhere bc_cmpid = 1 and bc_company = @branchname

if(CHARINDEX('经理',@title)>0) 
begin
select top 1 @order = ot_order ,
@displayLevel = '&emsp;&emsp;'
from  mps_title 
where CHARINDEX('经理',ot_title)>0
order by ot_id
end

if not exists (select 1 from mps_title where ot_title = @title)
insert into mps_title(ot_cmpid,ot_code,ot_title,ot_order,ot_level,ot_grade,ot_gradecaption,ot_tag,ot_display)
select 1, dbo.GetPY(@title),@title,@order,5,6,'员工',0,@displayLevel+@title

select @dpid = dp_id from mps_depart with(nolock) where dp_depart = @depart and dp_bcid =@bcid
select @otid = ot_id from mps_title with(nolock) where ot_title = @title

if not exists (select 1 from mps_depart_title where dt_dpid =@dpid and dt_otid = @otid )
insert into mps_depart_title(dt_dpid,dt_otid)
select @dpid,@otid

END