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)
)
as
begin

declare @sc nvarchar(4000)
set @sc = @scope 
if (len(@sc)<6) set @sc = '' 
declare @bizid int 
select @bizid = tmp_objid from mps_template where tmp_id=@tmpid 
update mps_template set tmp_scope=@sc where tmp_id=@tmpid 
if (@sc='') 
begin
delete mps_sql 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  from mps_template  where tmp_cmpid=@cmpid and tmp_type='biz' and tmp_objid=@bizid and  len(tmp_scope)>5 and tmp_caption<>'缺省模板' 

open c1
declare @tmp_id int 
declare @tmp_scope nvarchar(4000)  
declare @sql nvarchar(4000) 
set @sql = '' 
FETCH NEXT FROM c1 INTO @tmp_id,@tmp_scope 
WHILE (@@FETCH_STATUS = 0)
BEGIN
if (@sql>'') set @sql = @sql +';' 
 set @sql = @sql+'insert into @t(tmpid) select '+convert(varchar(10),@tmp_id)+
 ' from '+@dbname+@tbname+'  where ord_id={ordid} and ('+@tmp_scope+')'
 FETCH NEXT FROM c1 INTO @tmp_id,@tmp_scope 
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) ;'+@sql+
';declare @tmp int ; select @tmp=count(1) from @t; select @tmp tempcount ;'+
'if (@tmp=1) begin update '+@dbname+@tbname+' set ord_templateid=tmp_id , ord_template=tmp_caption from mps_template,@t where ord_id={ordid} and tmp_id=tmpid ;'+
'select a.* from mps_template a , @t where tmp_id=tmpid end else begin select 0 none where 1=0 end ; 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 where sql_cmpid=@cmpid and sql_name='g_form_subtemplate_'+convert(varchar(10),@bizid)
end

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

open c1
set @sql = '' 
FETCH NEXT FROM c1 INTO @tmp_id,@tmp_scope 
WHILE (@@FETCH_STATUS = 0)
BEGIN
if (@sql>'') set @sql = @sql +';' 
 set @sql = @sql+'insert into @t(tmpid) '+char(13)+
 'select '+convert(varchar(10),@tmp_id)+char(13)+
 'from '+@dbname+@tbname+char(13)+
 'where ord_id=@ordid and ('+@tmp_scope+')'
 FETCH NEXT FROM c1 INTO @tmp_id,@tmp_scope 
END
CLOSE c1
DEALLOCATE c1 
print @sql 
if (len(@sql)>10) 
begin
if (dbo.checkprocedure(@tbname+'_set_subform')=0) 
set @sql = 'alter procedure '+@tbname+N'_set_subform('+char(13)+
'@ordid bigint)'+char(13)+
'as'+char(13)+
'begin'+char(13)+
'declare @t table(tmpid int)'+char(13)+@sql+char(13) 
else 
set @sql = 'create procedure '+@tbname+N'_set_subform('+char(13)+
'@ordid bigint)'+char(13)+
'as'+char(13)+
'begin'+
'declare @t table(tmpid int)'+char(13)+@sql+char(13) 
set @sql = @sql+char(13)+
'declare @tmp int '+char(13)+
'select @tmp=count(1) from @t'+char(13)+
'if (@tmp=1) '+char(13)+
'update '+@dbname+@tbname+' set ord_templateid=tmp_id , ord_template=tmp_caption '+char(13)+
'from mps_template,@t '+char(13)+
'where ord_id=@ordid and tmp_id=tmpid ' +char(13)+
' end ' 
print @sql 
execute(@sql)
endelse 
begin
if (dbo.checkprocedure(@tbname+'_set_subform')=0)
begin
set @sql = 'drop procedure '+@tbname+'_set_subform' 
execute(@sql)
end
end
end