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

create procedure [dbo].[sys_sql_genfldvar](
@cmpid int,
@parseid int,
@procType varchar(50),  ----2017-8-9, 增加存储过程类型 ：bizbysrc, custfun
@procname varchar(100),
@blanks varchar(50),--前导空格
@script nvarchar(max) output,
@varList nvarchar(max) output,  --变量列表
@headDrop nvarchar(1000) output,
@sql_text varchar(2000) output ,  -- pub_sql 中存储过程定义
@sql_params varchar(2000) output,--pub_sql 中存储过程参数
@errmsg varchar(2000) output
)
as
begin
if @blanks is null
select @blanks = '  ',@varList=''
select @errmsg=''

DECLARE @heads varchar(500),@webtype varchar(50),@webname varchar(50),@script2 varchar(1000),@shortname varchar(20),
  @biztype varchar(20) , @bizid int ,@tagid int,@fldexp varchar(40),@dsname varchar(20),@fldvar varchar(50),
 @rfldname  varchar(20),@rdatatype  varchar(20),@rsize  int, @rprec  int,@i int,
 @s varchar(100),@cnt int,@pridsname varchar(20),@newValVar varchar(200),@tbfldid int,
 @paramvar varchar(50),@isString int,@fldID int,@fldDefs varchar(1000),@isDetailTable int,@declaredfld varchar(4000),
 @comVars varchar(4000),@prescript varchar(4000),@varname varchar(50),@isNew int

select @headDrop = 'if exists (select * from sysobjects where id = object_id(N''' +
@procname + ''') and OBJECTPROPERTY(id, N''IsProcedure'') = 1) ' + char(13) +
'  drop procedure ' + @procname + char(13) 
--0 生成过程头 
exec sys_Sql_genprochead @parseid,@procType,@procname,@heads output,@sql_text output,
 @sql_params output,@varList output,@comVars output,@prescript output

 
--1 Web变量，作为参数传递
if (@procType='java')
begin --1
Declare webvars Cursor local static  For
select webtype,webname,datatype,fldsize,fldprec
from sys_sql_web
where parseid=@parseid
OPEN webvars
Fetch next from webvars into @webtype, @webname, @rdatatype, @rsize,@rprec
While @@Fetch_status=0
begin --1.1
if (@rdatatype is null)
set @rdatatype='string'
if (@rsize is null)
set @rsize=20
if (@rprec is null)
set @rprec=2

 select @paramvar='@' +@webtype + '__' + @webname + '__'
 set @varname=@paramvar
 
select @isString=0
--整数
set @i=charindex('int',@rdatatype)
if (@i>0)
begin  --1.1.1
select @paramvar = @paramvar + ' int' + char(13)
select @sql_params=@sql_params + '|int'
end   -- end 1.1.1
set @i=charindex('date',@rdatatype)
if (@i>0)
begin --1.1.2
select @paramvar = @paramvar + ' datetime' + char(13)
select @sql_params=@sql_params + '|datetime'
end  -- end 1.1.2
set @i=charindex('decimal',@rdatatype)
if (@i>0)
begin --1.1.3
select @s=convert(varchar(2),@rprec)
if (@s is null)
set @s='2'
select @paramvar = @paramvar + ' decimal(18,' + @s+ ')' + char(13)
select @sql_params=@sql_params + '|decimal(18,' + @s+ ')'
end --end 1.1.3
set @i=charindex('string',@rdatatype)
if (@i>0)
begin --1.1.4
select @isString=1 --缺省为字符串型

if (@rsize is null or @rsize<2)
set @rsize=50

select @s=convert(varchar(4),@rsize)
if (@s is null)
set @s='50'
select @paramvar = @paramvar +   ' varchar(' + @s+ ')' + char(13)
select @sql_params=@sql_params + '|varchar(' + @s+ ')'
end --end 1.1.4

select @heads=@heads +',' +char(13)+ '    ' + @paramvar
if(@isString=0)
select @sql_text = @sql_text+ ',{' + @webtype + '__' + @webname + '__' + '}'
else
select @sql_text = @sql_text+ ',''{' + @webtype + '__' + @webname + '__' + '}'''

exec sys_sql_addvar  @varname,@rdatatype, @rsize,@rprec,@isNew output

Fetch next from webvars into @webtype, @webname, @rdatatype, @rsize,@rprec
end --end 1.1
close webvars
deallocate webvars
end -- end 1

select @heads=@heads + '  )' + char(13) + '  as ' + char(13) + ' BEGIN ' + char(13) 

--数据源变量定义
select @script = '',@pridsname=''

declare @defedDS varchar(1000),@dsVars varchar(4000),@tbname varchar(20)
select @defedDS='',@dsVars=''
Declare dsVar Cursor Static For
select dsname,fldDefs,tbname
from flatDSVars

OPEN dsVar
Fetch next from dsVar into @dsname,@fldDefs,@tbname
While @@Fetch_status=0
begin
set @i=charindex(@dsname,@defedDS)
if (@i>0)
goto nxt

select @varname= '@'+@tbname+'_rowcount__'

exec sys_sql_addvar @varname ,'int', 0,0,@isNew output

select @dsVars=@dsVars + char(13) +'   ,@'+@tbname +'_rowcount__ int,' + @fldDefs

nxt:
Fetch next from dsVar into @dsname,@fldDefs,@tbname
end

CLOSE dsVar
DEALLOCATE dsVar


exec sys_sql_addvar '@objid__','bigint',0,0,@isNew output
exec sys_sql_addvar '@parseid__','int',0,0,@isNew output
exec sys_sql_addvar '@sys_sql_isUpBizExisted__','int',0,0,@isNew output
exec sys_sql_addvar '@gotogen__','int',0,0,@isNew output
exec sys_sql_addvar '@revertTables__','varchar',4000,0,@isNew output  --回滚表
exec sys_sql_addvar '@hasUpDS__','int',0,0,@isNew output
exec sys_sql_addvar '@curbizid__','int',0,0,@isNew output
exec sys_sql_addvar '@curordid__','bigint',0,0,@isNew output
exec sys_sql_addvar '@curord2id__','bigint',0,0,@isNew output
exec sys_sql_addvar '@curbiztype__','varchar',20,0,@isNew output
exec sys_sql_addvar '@curtagid__','int',0,0,@isNew output
exec sys_sql_addvar '@lrevertTable__','varchar',100,0,@isNew output  --前次记录回滚表

exec sys_sql_addvar '@i__','int',0,0,@isNew output
exec sys_sql_addvar '@cnt__','int',0,0,@isNew output
select @script=@heads

declare @prescript2 varchar(4000)
exec sys_Sql_loadreplace @cmpid,@parseid,'  ',@prescript2 output

--生成变量定义
Declare @varDefScript varchar(max)
exec sys_sql_genvars @varDefScript output

if (@varDefScript is not null)
select @script = @script + @varDefScript

--特征代码
if (@prescript is not null)
select @script = @script + @prescript
 
--{}表达式处理:加载数据
if (@prescript2 is not null)
select @script = @script + @prescript2


select @script = @script +   '-- 上级业务类型存在标志--' + char(13)+   
'  select @sys_sql_isUpBizExisted__=0,@hasUpDS__=0,@gotogen__=0,@revertTables__='''' --缺省：上级业务类型不存在 ' + char(13)  


select @script = @script +
' set @parseid__=' + convert(varchar(20),@parseid) + '-- 保留变量' +  char(13)


--GLOBAL变量代码化
set @prescript = null 
exec sys_Sql_getval_global @parseid,@proctype,@prescript output
if (@prescript is not null)
select @script=@script + @prescript
  
--特殊表关键字段值获取：depart, branch
exec sys_Sql_getval_spkey @parseid ,@proctype ,@blanks , @prescript  output
if (@prescript is not null)
select @script=@script + @prescript

end