IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[sys_sql_deal_block]') AND type = 'P')
DROP PROCEDURE [dbo].[sys_sql_deal_block]
GO
--块处理 sys_sql_deal_block@cmpid,@parseid,@srcText,@preblank,@inCursorDS,@curtbname,@script output
create procedure [dbo].[sys_sql_deal_block](
@cmpid int,
  @parseid int,
@srcText varchar(4000),
@preblank varchar(50),
@inCursorDS varchar(20),      --当前所在游标DS
@curtbname varchar(50),       --当前游标tbname
@script varchar(max) output
) as
begin
declare @startInd int,@varType varchar(10),@varName varchar(20),@frmInd int,@toInd int,@targDs varchar(50),
@cstbname varchar(50),@predsname varchar(50),@curInd int,@srcLen int,@lastToInd int,@fromInd int,
@isRecursive int ,@isCurDetailRec int,@tmpS varchar(max),@webType varchar(20),@webName varchar(50),
@opType varchar(20),@script2 varchar(max),@targetDS varchar(20),@hasUpDS int,@scriptCount int,@targTbname varchar(20)

--目标数据源
select @fromInd=1,@curInd=1, @varType='',@varName='',@srcLen=len(@srcText),@script=''

 --对源代码过滤处理
-- 下面循环，每次处理一个系统变量：包括web,update,insert
 
 --对源代码过滤处理
-- 下面循环，每次处理一个系统变量：包括web,update,insert
while (@curInd<@srcLen)
begin-- 1

exec sys_sql_getNextSysVar @srcText,@curInd,@varType output,@varName output,@fromInd output ,@toInd output
--结束判断
if (@varType is null or @varName is null) -- 为空，忽略该变量
goto nxt

set @lastToInd = @toInd

 --首先copy没有匹配的前部
 select @tmpS = substring(@srcText, @curInd,@fromInd - @curInd )
 if (@tmpS is not null)
 select @script=@script + @tmpS

-- web 变量处理 begin
if (@varType='web')
begin
select @webType=webtype,@webName=webname
from sys_sql_web
where fullname=@varName
--用变量替换
select @script=@script + '@'+@webType + '__' + @webName
goto nxt
end
--web 变量处理 end

-- field 变量处理 begin
if (@varType='field')
begin
select @webName=fldvar
from flatFld
where fldname=@varName  
--用变量替换
if (@webName is not null)
select @script=@script +@webName
else
select @script=@script + ' replace ERROR !!! ' 

goto nxt
end
--field 变量处理 end
-- repl 变量处理 begin
if (@varType='repl')
begin
exec sys_Sql_genReplace @parseid,@varname,@script2 output
if (@script2 is not null)
select @script=@script + @script2
 
goto nxt
end

--parseid
select @parseid=convert(int,@varname)  --这里需要完善：如果@varname不是整数，系统崩溃？
--操作类型
select @opType=opType
from sys_sql_inf
where parseid=@parseid

select @script = @script + '-- parseid : ' + convert(varchar(10),@parseid) + '--' + char(13) + char(13)

   
-- 目标数据源
declare @tds varchar(20),@fInd int ,@tInd int
select @tds=tTable,@optype=optype
from sys_sql_inf
where parseID=@parseID

--2017-8-9 action,error
if (@optype='wfaction' or @optype='wferror')
begin 
exec sys_sql_act_err @parseid,@preblank,@script2 output
if (@script2 is not null)
select @script=@script + @script2
end

--2017-8-11 log
if (@optype='log' )
begin 
exec sys_sql_log @parseid,@preblank,@script2 output
if (@script2 is not null)
select @script=@script + @script2
end

if (@optype='loaddata' )
begin 
set @script2=''
--exec sys_sql_command_loaddata @parseid,@preblank,@script2 output
if (@script2 is not null)
select @script=@script + @script2
end

if (@optype='cursor')
begin
--生成游标命令脚本
exec sys_sql_command_cursor @parseid,@preblank,@script2 output

if (@script2 is not null)
select @script=@script + @script2
end

--数据库修改与记录增加
if (@optype='update' or @optype='insert')
begin

--生成批次
  exec sys_sql_genbatchid @parseid
 
 --初始设置，如果存在上级，设置变量
 if (exists (select * from flatDSS where shortname='上级'))
 select @script = @script + ' select @hasUpDS=1' + char(13)
 
exec sys_sql_getNextSysVar @tds,1,@varType output,@varName output,@fInd output ,@tInd output
select @targetDS=@varName

select @script =  @script + @tmpS
 
  select @script = @script + '-- update,insert  --- ' + char(13) 
  
-- exec sys_sql_cmd_upin @cmpid,@parseid,@targetDS,@targetTb,@preBank,@loadedGetOrder,@opTyp,@script  output 
  select @targTbname=tbname from flatDSS
  where parseid=@parseid and dsname=@targetDS 
   exec  sys_sql_cmd_upin @cmpid,@parseid,@targetDS,@targTbname,@preblank,0,@optype,@script2 output

if (@script2 is not null)
select @script =  @script + @script2
 end


 --下个变量
 nxt:
 select @curInd = @toInd ,@scriptCount=@scriptCount + 1
end  --//end 1 
--最后非变量处理
 if (@lastToInd<@srcLen)
 select @script=@script  + substring(@srcText, @lastToInd+1,@srcLen)

-- 过程以 END 结束

end