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

create procedure [dbo].[sys_sql_genInsert](
@parseid int,
@ttbname varchar(20),--物理表名
@preBlank varchar(100),
@script varchar(max) output
)
as
begin
set @script=@preBlank + '' + char(13) + '  --新增记录  ' + char(13)
 

Declare @whrCond varchar(1000),@batchCond varchar(1000),@condname varchar(20),
@varName char(10),@varType varchar(10),
@frmInd int,@toInd int,@tTable varchar(20), @batchid int,@keyfld varchar(20)

Declare @dsname varchar(20),@fldname varchar(20),@tbname varchar(20),@tbfld varchar(20),
@fldvar varchar(50),@fldvarnv varchar(50),@fldval varchar(1000),@repval varchar(1000),
@scriptNV nvarchar(max),@tbfldid int,@flds varchar(4000),@vals varchar(4000),@fldexp varchar(40)

set @scriptNV=''
select @tTable=tTable,@whrCond=whrCond
from sys_sql_inf
where parseid=@parseid
-- 抽取更新表数据源
exec sys_sql_getNextSysVar @tTable,1,@varType output,@varName  output,@frmInd output,@toInd output
--@exp ,@startInd,@varType,@varName ,@frmInd,@toInd output
/*
-- 5.2 目标数据源的物理表
if (@varType = 'ds' and @varName is not null)
select @ttbname=tbname
from #finalDS2Table
where dsname=@varName
*/

select @whrCond=whrcond
from sys_sql_inf
where parseid=@parseid
if (@whrcond is null)
set @whrcond=''

-- 5.3 按批次生成 insert 语句
Declare upbatch Cursor For
select distinct batchid 
from sys_sql_fld
where parseid=@parseid and dsname=@varName and isforupdate=1

Open upbatch
Fetch Next From upbatch into @batchid
While @@Fetch_status=0  --5.3.1 批循环
begin
select @flds='',@vals=''

--5.3.2更新表
if (@ttbname is not null)
select @script = @script +
' insert into ' + @ttbname + '(' 
-- 5.3 字段 游标
declare upfld Cursor For
select dsname,fldname,fldval,condname
from sys_sql_fld
where parseid=@parseid and dsname=@varName and isforupdate=1 and batchid=@batchid
Open upfld

Fetch Next from upfld into @dsname,@fldname,@fldval,@condname
-- 循环，每次处理一个字段
declare @fldcount int = 0
while @@fetch_status=0
begin
--获得物理字段名称
select @tbfld=tbfld,@fldvarnv = fldvarnv,@tbfldid=tbfldid,@fldvar=fldvar
from flatFld
where tbname=@ttbname and fldname=@fldname

if (@tbfld is null)
set @tbfld=@fldexp


--求表达式值
exec sys_sql_replaceExp @parseid,@fldval,@repval output
if (@repval is null)  --为空，忽略本字段
goto nxt;
 
--为新值变量赋值
select @scriptNV=@scriptNV +
@preBlank +'  select ' + @fldvarnv +'=' +@repval + char(13) 
/*  新增记录，不记字段日志
  @preBlank +'  insert into ' + 'zog_'+@ttbname + '(jobid,objectid,fldid,oldval,newval,isreversed,optype)' + char(13) +
@preBlank +'   values(@jbid, @inIdentity' +
  ',' + case  @tbfldid when null then 'null' else convert(varchar(15),@tbfldid) end +  -- 字段id
',' + @fldvar + 
',' + @fldvarnv + 
',0,1)' + char(13) 
*/
if (@fldcount = 0)
select @flds = @tbfld,@vals=@repval
else
select @flds = @flds + ',' + @tbfld,@vals=@vals + ',' +@repval 

-- 下一字段
select @fldcount=@fldcount+1
nxt:
Fetch Next from upfld into @dsname,@fldname,@fldval,@condname
end

select @script = @script + @flds + ')' + char(13) +
@preBlank + ' values(' + @vals + ')' + char(13) +
@preBlank + ' -- ' + char(13) +
@preBlank + 'set @inIdentity__=@@identity '+char(13) +

--新增记录ID日志
  @preBlank +'  insert into ' + 'zog_'+@ttbname + '(jobid,objectid,fldid,oldval,newval,isreversed,optype)' + char(13) +
@preBlank +'   values(@jbid__, @inIdentity__' +
  ',' + case  @tbfldid when null then 'null' else convert(varchar(15),@tbfldid) end +  -- 字段id
',null' + 
',@inIdentity__ ' + 
',0,1)' + char(13) 

close upfld
deallocate upfld

-- 下一批次 update
Fetch Next From upbatch into @batchid

end -- 5.3.1 批循环结束

if (@scriptNV is not null and len(@scriptNV)>6)
begin 
select @scriptNV = @scriptNV  + 
@preBlank +'if (not @lreverttable__=''' +@ttbname  + ''')' +char(13) +   --涉及到的
@preBlank +'  select @revertTables__ = @revertTables__ + ''' + @ttbname + ','',@lreverttable__=''' +@ttbname  + '''' +char(13)
select @script=@script +char(13) + @scriptNV
end 
Close upbatch
Deallocate upbatch

end