IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[sys_sql_genUpdate]') AND type = 'P')
DROP PROCEDURE [dbo].[sys_sql_genUpdate]
GO
create procedure [dbo].[sys_sql_genUpdate](
@parseid int,
@ttbname varchar(20),--物理表名
@preBlank varchar(100),
@script varchar(4000) output
)
as
begin

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
set @script='' + char(13) + @preBlank + '  --更新数据库  ' + char(13)

--检查表名、字段名是否完整
declare @haserr int
exec sys_sql_errcheck @parseid,@haserr output
if (@haserr=1)
begin
select @script = @script + @preBlank + ' -- 表或字段有错，不能生成语句 ' + char(13) +
@preBlank + ' set @i__=0' + char(13)

return
end
Declare @whrCond varchar(1000),@whrLnk varchar(1000), @batchCond varchar(1000),@batchLnk varchar(1000),@condname varchar(20),
@varName char(10),@varType varchar(10),@batchSql varchar(2000),@whrName varchar(20),
@whrlnkval varchar(1000),@whrcondval varchar(1000),@batchlnkval varchar(1000),@batchcondval varchar(1000),
@frmInd int,@toInd int,@tTable varchar(20), @batchid int,@keyfld varchar(20)

Declare @dsname varchar(20),@fldname varchar(50),@tbname varchar(20),@tbfld varchar(20),@fldexp varchar(40),
@fldvar varchar(50),@fldvarnv varchar(50),@fldval varchar(1000),@repval varchar(1000),
@scriptNV nvarchar(max),@tbfldid int, @batchCond2 varchar(1000),@err varchar(4000),
@tbDatabase varchar(20)
--修改前字段值
Declare @oldVal varchar(max)
select  @oldVal='',@scriptNV=''

select @keyfld=keyfld,@tbDatabase=tbDatabase
from flatDSS
where parseid=@parseid and tbname=@ttbname
if (@tbDatabase is null)
set @tbDatabase=''

select @tTable=tTable,@whrName=whrCond  --全局条件名称
from sys_sql_inf
where parseid=@parseid

-- 全局条件
if (@whrName is not null) --全局条件
begin
--取连接关系
select @whrcond=condval
from sys_sql_cond
where parseid=@parseid and condname=@whrName

--求表达式值
set @whrcondval=null
if (@whrcond is not null)
exec sys_sql_replaceExp @parseid,@whrcond,@whrcondval output

select @whrcond=@whrcondval
end -- 全局条件
if (@whrcond is null)
set @whrcond=''

-- 抽取更新表数据源
exec sys_sql_getNextSysVar @tTable,1,@varType output,@varName  output,@frmInd output,@toInd output
--@exp ,@startInd,@varType,@varName ,@frmInd,@toInd output


-- 5.3 按批次生成 update 语句
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 @batchSql='',@scriptNV='',@batchCond='',@batchCond2 =''
--5.3.2更新表
if (@ttbname is not null)
select @batchSql = @batchSql +
@preBlank+' update ' + @tbDatabase + @ttbname + char(13) 
-- 5.3 字段 游标
declare upfld Cursor For
select dsname,fldname,fldval,condname,fldexp
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,@fldexp
-- 循环，每次处理一个字段
declare @fldcount int = 0
while @@fetch_status=0   --5.3.4 循环字段
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) +char(13)+
@preBlank +'  if (@cnt__>0) ' +char(13)+
  @preBlank +'    insert into ' + 'zog_'+@ttbname + '(jobid,objectid,fldid,oldval,newval,isreversed,optype)' + char(13) +
@preBlank +'      values(@jbid, @'+@ttbname + '_' +@keyfld+'__' +
  ',' + case  @tbfldid when null then 'null' else convert(varchar(15),@tbfldid) end +  -- 字段id
',' + @fldvar + 
',' + @fldvarnv  +
',0,1)' + char(13) +char(13) 

if (@fldcount = 0)
select @batchSql = @batchSql +
@preBlank +'       set ' + @tbfld + '=' + @repval + char(13)
else
select @batchSql = @batchSql +
@preBlank+'            ,' + @tbfld + '=' + @repval + char(13)


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

--批条件
--取批条件

if (@condname is not null and len(@condname)>2)
begin
select @batchcondval=condval
from sys_sql_cond
where parseid=@parseid and condname=@condname
--求表达式值
set @batchcond=null
if (@batchcondval is not null)
exec sys_sql_replaceExp @parseid,@batchcondval,@batchcond output

end  --
if (@batchcond is null)
set @batchcond=''
-- 合并条件
if (@batchcond is not null and len(@batchcond)>2)
begin
if (len(@whrCond)>2)
select @batchCond = @whrCond +' and ' + @batchCond
end
else
select @batchCond = @whrCond 

--求表达式值
set @batchCond2=null
exec sys_sql_replaceExp @parseid, @batchCond,@batchCond2 output


if (@batchCond2 is not null and len(@batchCond2)>2)
select @batchSql = @preBlank + '  if (' +@batchCond2+')' + char(13)+
  @preBlank + '  begin' +char(13) +
  @preBlank + '    ' + @batchSql

select @batchSql = @batchSql +
@preBlank + '        where ' + @keyfld + '=@'+@ttbname+'_' + @keyfld +'__' + char(13)


close upfld
deallocate upfld

if (@batchSql is not null)
select @script = @script +
@preBlank+'    select @cnt__ = 0 ' +char(13) + 
@batchSql + char(13) +
@preBlank+'    select @cnt__ = @@rowcount ' +char(13) + 
@scriptNV 
if (@batchCond2 is not null and len(@batchCond2)>2)
select @script = @script +
@preBlank+'  end  ' +char(13)

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

end -- 5.3.1 批循环结束


Close upbatch
Deallocate upbatch
--回退表
select @script = @script +char(13) +
@preBlank +'if (not @lreverttable__=''' +@ttbname  + ''')' +char(13) +   --涉及到的
@preBlank +'  select @revertTables__ = @revertTables__ + ''' + @ttbname + ','',@lreverttable__=''' +@ttbname  + '''' +char(13)

if (@script is null)
begin
select @err=convert(varchar(20),@parseid) + '生成数据操作语句出错!'
exec sys_Sql_newerr @parseid,@err
if (@ttbname is null)
set @ttbname='NULL'
select @script=
@preBlank +'-- ' + @err  + ' @ttbname= ' + @ttbname + char(13) +
@preBlank +'set @i__=0 '  + char(13)
end

end