IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[Sys_sql_genScript]') AND type = 'P')
DROP PROCEDURE [dbo].[Sys_sql_genScript]
GO

Create procedure [dbo].[Sys_sql_genScript](
@recallTimes int,--
@cmpid int,
@parseid int ,--主parsseID，存储了预分析代码
@procType varchar(50),--2017-8-9, 增加存储过程类型 ：bizbysrc，
@script nvarchar(max) output,
@errmsg varchar(2000) output
)
as
BEGIN
--Declare @script nvarchar(max)
set nocount on
 
DECLARE @srcText nvarchar(max),@opTyp varchar(10),@dschain varchar(100),@heads nvarchar(1000)
     ,@thisbiztype varchar(20),@thisbizid int,@preBank varchar(50),@procname varchar(100),@tmpS varchar(max)
     ,@script2 varchar(max),@isCurDetailRec int,@callFmt varchar(4000)

select @srcText=parsedscript,@thisbiztype=thisbiztype,@thisbizid=thisbizid,
@preBank=' ',@procname=procname
from sys_sql_inf 
where @parseid=parseid

select @script = '',@errmsg=''


Declare @fldNames varchar(100),@fldvar varchar(200),@curInd int,@srcLen int, @scriptCount int,@targetDS varchar(20),
 @varType varchar(10), @varName varchar(20),@fromInd int,@toInd int,@lastToInd int,@hasUpDS int,@hasUpDS2 int,
 @varList varchar(1000),@webType varchar(20),@webName varchar(40),@sql_text varchar(2000),@sql_params varchar(2000)
 
 select @curInd = 1,@lastToInd=1,@scriptCount=0, @srcLen=len(@srcText)
 
 --0 准备数据
 exec sys_sql_dealdata @cmpid ,@parseid,@thisbiztype,@thisbizid ,@procType,@errmsg output
 --错误检查
 if (len(@errmsg)>0)
 begin
 print @errmsg
 return 
 end
  
--变量生成
exec sys_sql_genfldvar @cmpid,@parseid,@procType,@procname,'',
  @script2  output,@varList output,@heads output,@sql_text output,@sql_params output,@errmsg output
 --错误检查
 if (len(@errmsg)>0)
 begin
 print @errmsg
 return 
 end
 
if (@script2 is not null)
select @script = @script2 + @script
 
 --对源代码过滤处理
-- 下面循环，每次处理一个系统变量：包括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) -- 为空，忽略该变量
break

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

-- 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

--field 变量处理 end

--parseid
select @parseid=convert(int,@varname)
--操作类型
select @opTyp=opType
from sys_sql_inf
where parseid=@parseid

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

  --生成批次
  if (@scriptCount>0)
  exec sys_sql_genbatchid @parseid
 
 --初始设置，如果存在上级，设置变量
 if (exists (select * from flatDSS where shortname='上级'))
 select @script = @script + ' select @hasUpDS__=1' + char(13)
   

if (@scriptCount>0)
begin  --2
-- 目标数据源
declare @tds varchar(20),@fInd int ,@tInd int,@optype varchar(10)
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,@prebank,@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,@prebank,@script2 output
if (@script2 is not null)
select @script=@script + @script2
end

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

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

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

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


exec sys_sql_getNextSysVar @tds,1,@varType output,@varName output,@fInd output ,@tInd output
select @targetDS=@varName

exec sys_sql_cmd_upin @cmpid,@parseid,@targetDS,null,@prebank,0,@opTyp,@script2  output

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


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

-- 过程以 END 结束

select @script = @script + char(13) +
'  --回滚表 ' + char(13) +
'  if (@revertTables__ is not null) ' + char(13) +
'    insert into sys_sql_revtables(jobid,tbname) values(@jbid,@revertTables__)' + char(13) + 
' -- 正常运行结束 -- ' + char(13) +
'  lblEnd:' +char(13) 

if (exists(select * from flatDSS where shortname='上级' or shortname='根'))
select @script = @script + char(13) +
' if (@gotogen__=1) ' + char(13) +
'   goto regenscript ' + char(13) + 
'  return ' + char(13)

if (exists(select * from flatDSS where shortname='上级' or shortname='根'))
select @script = @script + 
'-- 代码重新生成部分　－－ ' + char(13) +
'  -- 从新生成存储过程 --' +char(13) + 
'  regenscript: ' + char(13) +
 '  set @recallTimes__=@recallTimes__ + 1 ' + char(13) +
 '  if (@recallTimes>10)' + char(13) +
 '  begin' + char(13) + 
 '    insert into sys_sql_err(cmpid,parseid,errmsg,errlevel) values(' +
 convert(varchar(10),@cmpid)+',' + convert(varchar(10),@parseid) + ',''递归生成存储过程错误'' ,100)'   + char(13) +
 '    return '+char(13) +
 '  end ' + char(13) +
 '  exec Sys_sql_genScript @recallTimes__, @cmpid,@parseid__,@script__ output ' + char(13) +
 '  -- 再执行存储过程 ' + char(13) + 
 '  exec  ' + @procname + '  ' + @varList + char(13) 

select @script = @script + char(13) +   
'END' +char(13)

--7 生成存储过程
exec(@heads)
exec(@script)

select @sql_text =  @procname + '  ' + @sql_text
update mps_sql
set sql_text=@sql_text,
sql_params=@sql_params
where sql_cmpid=@cmpid  and sql_name=@procname
if (@@ROWCOUNT=0)
insert into mps_sql(sql_cmpid, sql_name,sql_text,sql_params)
values(@cmpid,@procname,@sql_text,@sql_params)



END