IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_gen_shpmodify]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_gen_shpmodify]
GO
create procedure [dbo].[mps_gen_shpmodify](
@cmpid int
)
as
begin
set nocount on 
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED 
declare @s1 nvarchar(100)  
declare @tbname varchar(50)
declare @dbname varchar(50)
declare @fkey   varchar(50) 

exec sys_sql_getTableNamePk @cmpid,'shp',@cmpid,0,@tbname output,@fkey output ,@dbname output 
declare @ss nvarchar(MAX) 
declare @sql nvarchar(MAX) 
declare @procparam nvarchar(MAX) 
set @sql = N'
-- 生成时间:'+convert(varchar(20),getdate(),120)+N' by ebs-core  
'
if (dbo.checkprocedure(@tbname+'_shpmodify')=0) set @sql=@sql+'alter ' else set @sql=@sql+'create '
set @sql = @sql+' procedure '+@tbname+N'_shpmodify(
@dpid bigint, 
@cmpid int,
@branchid int,
@userdpid int,
@useruid int,
@userotid int, '+char(13)+char(10)
    set @s1 = @dbname+@tbname  
    exec mps_gen_proc_params @s1,'dp_id,dp_cmpid',@procparam output 
--- 生成参数清单
if (len(@procparam)<=6) set @procparam = '@sys_nouse_v1 int' 
set @sql = @sql+@procparam+N' ) 
as  
begin  
set nocount on   
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED 
declare @nonedate datetime 
set @nonedate = CONVERT(datetime,''1910-1-1'')
 declare @operation varchar(50)
declare @errorinfo nvarchar(4000) 
 set @operation = ''continue'' 
set @errorinfo = '''' 
if (dbo.mps_sys_checkkeywords(''shp.check'',@cmpid,0,'''','''')=0)
begin
exec mps_depart_shpcheck @cmpid,@branchid,@userdpid,@useruid,@userotid,@dpid,@operation output,@errorinfo output
if @operation = ''abort''
begin
select @operation operation, @errorinfo errorinfo
return 
end
end

declare @existcode int,@exbcid int
if @dp_code != ''_none_''
begin
select @exbcid = dp_bcid from mps_depart where dp_id = @dpid
select @existcode = count(*) from mps_depart  where dp_cmpid = @cmpid and dp_code = @dp_code and dp_id != @dpid
if @existcode>0
begin
select ''error'' error,''此公司已存在门店编号：''+@dp_code as errorinfo
return 
end
end

 --- 以下是预处理事件中的临时表
 '

 if (dbo.mps_sys_checkkeywords('shp.beforemodify,shp.aftermodify',@cmpid,0,'','')<>0)   -- 没有事件
 begin
 exec mps_sys_gen_updatestatement @s1,'','@','dp_id,dp_cmpid',@ss output 
 set @sql = @sql+N'update '+@dbname+@tbname+' set '+char(13)+char(10)+@ss+char(13)+char(10) + 
 'where dp_id=@dpid ' 
 end else 
 begin
 exec mps_sys_gen_updatestatement @s1,'','@','dp_id,dp_cmpid',@ss output 
 if (len(@ss)>10) 
 set @sql = @sql+char(13)+char(10)+
 'exec drop_temp_table ''#old'' '+char(13)+char(10)+
'select * into #old from '+@dbname+@tbname+N' where dp_id=@dpid '+char(13)+char(10)+
'exec drop_temp_table ''#new'' '+char(13)+char(10)+
'select *  into #new from #old '+char(13)+char(10)+
'update #new set '+char(13)+char(10) +  @ss + char(13)+char(10) 
 end 
 
 if (dbo.mps_sys_checkkeywords('shp.beforemodify',@cmpid,0,'','')=0) 
 begin
 set @sql = @sql+'exec '+@tbname+'_shpmodify_before @cmpid,@branchid,@userdpid,@useruid,@userotid,@dpid,@operation output,@errorinfo output   ' 
 end
 
 set @sql = @sql +N'
 if (@operation=''continue'') 
 begin
 set @nonedate = @nonedate 
 '
 if (dbo.mps_sys_checkkeywords('shp.beforemodify,shp.aftermodify',@cmpid,0,'','')=0)
 begin
 set @s1 = @dbname+@tbname  
 exec mps_sys_gen_updatestatement @s1,'old','new','dp_id,dp_cmpid',@ss output 
 if (len(@ss)>10) 
 set @sql=@sql+char(13)+char(10)+
'update  old set '+char(13)+char(10)+@ss+''+char(13)+char(10)+
 'from '+@dbname+@tbname+' old,#new new where old.dp_id=new.dp_id ' 
 end
 if (dbo.mps_sys_checkkeywords('shp.aftermodify',@cmpid,0,'','')=0) 
 begin
 set @sql = @sql+char(13)+char(10)+
 'exec '+@tbname+'_shpmodify_after @cmpid,@branchid,@userdpid,@useruid,@userotid,@dpid '+char(13)+char(10)
 end
 if (dbo.mps_sys_checkkeywords('shp.aftermodify',@cmpid,0,'#new','update')=0) 
 begin
 set @s1 = @dbname+@tbname  
 exec mps_sys_gen_updatestatement @s1,'old','new','dp_id,dp_cmpid',@ss output 
 if (len(@ss)>10) 
 set @sql=@sql+char(13)+char(10)+
'update  old set '+char(13)+char(10)+@ss+' '+char(13)+char(10)+
 'from '+@dbname+@tbname+' old,#new new where old.dp_id=new.dp_id ' 
 end
 set @sql=@sql+N'
if(@dp_fullname='''' or @dp_fullname is null or @dp_fullname = ''_none_'')
begin
updatemps_depart set dp_fullname = case when dp_fullname = '''' or dp_fullname = ''_none_'' then @dp_depart else @dp_fullname end
where dp_id = @dpid
end
if(@dp_display='''' or @dp_display is null or @dp_display = ''_none_'')
begin
updatemps_depart set dp_display = case when dp_display = '''' or dp_display = ''_none_'' then @dp_depart else @dp_display end
where dp_id = @dpid
end
 select ''ok'' operation, '''' errorinfo 
end  else 
begin
select @operation operation, @errorinfo errorinfo  
end
' 
if (dbo.mps_sys_checkkeywords('shp.beforemodify,shp.aftermodify',@cmpid,0,'','')=0)
 begin
 set @sql=@sql+char(13)+char(10)+
'exec drop_temp_table ''#new'' '+char(13)+char(10)+ 
'exec drop_temp_table ''#account'' '+char(13)+char(10)+ 
'exec drop_temp_table ''#old'' '+char(13)+char(10)
 end
set @sql=@sql+N'

if(@dp_fdpid>0)
begin
delete from mps_depart_title where dt_dpid = @dpid
insert into mps_depart_title(dt_dpid,dt_otid)
select @dpid,dt_otid from mps_depart_title where dt_dpid = @dp_fdpid
end

 
set nocount off 
end '
exec myprint @sql 
execute(@sql)  
-- 生成mps_sql 
set @sql = ' exec mps_sys_gen_mpssql '+CONVERT(varchar(10),@cmpid)+','''+@tbname+N'_shpmodify'',
''@dpid bigint,@cmpid int,@branchid int,@userdpid int,@useruid int,@userotid int,'+@procparam+'''' 
exec myprint @sql 
execute(@sql)
set nocount off 
end