IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_save_title]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_save_title]
GO
CREATE procedure [dbo].[mps_save_title]
 -- Add the parameters for the stored procedure here
 @cmpid int,
 @ot_id int,
 @ot_code varchar(20),
 @ot_title nvarchar(50),
 @ot_grade int,
 @ot_gradecaption nvarchar(50),
 @ot_order int
AS
BEGIN
 -- SET NOCOUNT ON added to prevent extra result sets from
 -- interfering with SELECT statements.
 SET NOCOUNT ON;

 declare @rowcountnum int = 0
    -- Insert statements for procedure here
 UPDATE mps_title SET
 ot_code = CASE WHEN @ot_code = '_none_' THEN ot_code ELSE @ot_code END,
 ot_title = CASE WHEN @ot_title = '_none_' THEN ot_title ELSE @ot_title END,
 ot_grade = CASE WHEN @ot_grade = -999999999 THEN ot_grade ELSE @ot_grade END,
 ot_order = CASE WHEN @ot_order = -999999999 THEN ot_order ELSE @ot_order END,
 ot_gradecaption = CASE WHEN @ot_gradecaption = '_none_' THEN ot_gradecaption ELSE @ot_gradecaption END
 WHERE ot_id = @ot_id AND ot_cmpid = @cmpid

 update mps_employee
 set cu_title = ot_title
 from mps_title with(nolock)
 where ot_id = cu_otid
 and ot_id = @ot_id

 update mps_login
 set cu_title = ot_title
 from mps_title with(nolock)
 where ot_id = cu_otid
 and ot_id = @ot_id

 update mps_login_role
 set lgu_title = ot_title
 from mps_title with(nolock)
 where ot_id = lgu_otid
 and ot_id = @ot_id




 set @rowcountnum = @@ROWCOUNT

 --SELECT @rowcountnum NONE WHERE 0=1

 SELECT @rowcountnum result

 EXEC mps_update_titleinfo @cmpid
END