IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_save_formscope]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_save_formscope]
GO
CREATE procedure [dbo].[mps_save_formscope] 
(
@cmpid int,
@tmpid int,
@scope nvarchar(4000),
@mdindex int,
@mdwrks varchar(1000) 
)
as
begin

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
declare @sc nvarchar(4000)
set @sc = @scope 
if (len(@sc)<6) set @sc = '' 
declare @bizid int 
select @bizid = tmp_objid from mps_template with(nolock) where tmp_id=@tmpid 
update mps_template with(rowlock) set tmp_scope=case when @sc IS null or @sc='_none_'  then tmp_scope else @sc end ,
tmp_main = case when @mdindex=-999999999 then tmp_main else @mdindex end ,
tmp_wrks = case when @mdwrks IS null or @mdwrks='_none_' then tmp_wrks else @mdwrks end 
 where tmp_id=@tmpid 
if (@sc='') 
begin
delete mps_sql with(rowlock)  where sql_cmpid=@cmpid and sql_name='g_form_subtemplate_'+convert(varchar(10),@bizid)
end

declare @tbname varchar(50)
declare @dbname varchar(50)
declare @fkey   varchar(50) 
exec sys_sql_getTableNamePk @cmpid,'biz',@bizid,0,@tbname output,@fkey output ,@dbname output 

declare c1 cursor LOCAL FORWARD_ONLY READ_ONLY for
select tmp_id,tmp_scope ,  tmp_wrks from mps_template with(nolock)  where tmp_cmpid=@cmpid and tmp_type='biz' and tmp_objid=@bizid and  (len(tmp_scope)>5 or tmp_wrks>'') and tmp_caption<>'缺省模板' and tmp_main>0

 

open c1
declare @tmp_id int 
declare @tmp_scope nvarchar(4000)  
declare @tmp_wrks varchar(1000) 
declare @sql nvarchar(4000) 
set @sql = '' 
FETCH NEXT FROM c1 INTO @tmp_id,@tmp_scope ,  @tmp_wrks 
WHILE (@@FETCH_STATUS = 0)
BEGIN
set @tmp_scope = replace(@tmp_scope,'@useruid','{useruid}')
set @tmp_scope = replace(@tmp_scope,'@userdpid','{userdpid}')
set @tmp_scope = replace(@tmp_scope,'@branchid','{branchid}')
set @tmp_scope = replace(@tmp_scope,'@useradmin','{useradmin}')
set @tmp_scope = replace(@tmp_scope,'@usertype','{usertype}')
set @tmp_scope = replace(@tmp_scope,'@userotid','{userotid}')
set @tmp_scope = replace(@tmp_scope,'@userctid','{userctid}')
set @tmp_scope = replace(@tmp_scope,'@cmpid','{cmpid}')
set @tmp_scope = replace(@tmp_scope,'@ordid','{ordid}')
set @tmp_scope = replace(@tmp_scope,'@wrkid','{wrkid}')
set @tmp_scope = replace(@tmp_scope,'@bizid',convert(varchar(10),@bizid))
set @tmp_scope = replace(@tmp_scope,'@wsid','{wsid}')
set @tmp_wrks = isnull(@tmp_wrks,'')
if (@tmp_wrks=',') set @tmp_wrks = ''
if (@tmp_wrks=',,') set @tmp_wrks = ''
if (@tmp_wrks!='') set @tmp_wrks=','+@tmp_wrks+',' 
if (@sql>'') set @sql = @sql +';' 
 set @sql = @sql+'insert into @t(tmpid,ismain) select '+convert(varchar(10),@tmp_id)+
 ' , 1 from '+@dbname+@tbname+' with(nolock)  where ord_id={ordid} ' 
 if (@tmp_scope>'') set @sql  = @sql +' and ('+@tmp_scope+')' 
 if (@tmp_wrks>'') set @sql  = @sql +' and (charindex('',{wrkid},'','''+@tmp_wrks+''')>0)' 
 FETCH NEXT FROM c1 INTO @tmp_id,@tmp_scope  , @tmp_wrks 
END
CLOSE c1
DEALLOCATE c1 
if (len(@sql)>10) 
begin
delete mps_sql where sql_cmpid=@cmpid and sql_name='g_form_subtemplate_'+convert(varchar(10),@bizid)
set @sql = 'set nocount on ;declare @t table(tmpid int , ismain int) ;'+@sql+
';select 0 none , 1 none2 where 0=1;'+
'select a.* from mps_template a with(nolock) , @t where tmp_id=tmpid and  ismain>0 ; set nocount off ;'  
insert into mps_sql(sql_cmpid,sql_type,sql_name,sql_text) 
select @cmpid,'MSSQL','g_form_subtemplate_'+convert(varchar(10),@bizid),@sql 
endelse 
begin
delete mps_sql  with(rowlock) where sql_cmpid=@cmpid and sql_name='g_form_subtemplate_'+convert(varchar(10),@bizid)
end

print @sql  
 
if (dbo.checkprocedure(@tbname+'_set_subform')=0)
begin
set @sql = 'drop procedure '+@tbname+'_set_subform' 
execute(@sql)
end

end