IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[sys_sql_genparams]') AND type = 'P')
DROP PROCEDURE [dbo].[sys_sql_genparams]
GO
--生成明细表后级取值存储过程的参数
create procedure [dbo].[sys_sql_genparams] (
@parseid int,
@predsname varchar(20),--明细表dsname
@recursLevel int,  --递归级别，第一次调用未0
@defParams varchar(1000) output,   --过程定义参数列表
@callParams varchar(1000) output,  --过程调用参数列表
@procname varchar(100) output,--存储过程名称
@localvar varchar(max) output--存储过程局部变量定义表
)
as
begin

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
Declare @dsname varchar(20), @dealtDS varchar(200),@biztype varchar(20),@s varchar(100)
,@bizid int,@tagid int,@preLnkFld varchar(20),@keyfld varchar(20),@prekeyfld varchar(20),@shortname varchar(20)
,@i int,@nextDef varchar(1000),@nextCall varchar(1000), @fldDef  varchar(50),@fldDef_N  varchar(50),@nextProc varchar(1000)
,@tbfld varchar(20),@fldvar varchar(20),@datatype varchar(20),@fldsize int,@fldprec int,@nextlocal varchar(max)

DECLARE @heads varchar(500),@webtype varchar(50),@webname varchar(50),@script2 varchar(1000),
 @fldexp varchar(40),
 @rfldname  varchar(20),@rdatatype  varchar(20),@rsize  int, @rprec  int,
 @cnt int,@pridsname varchar(20),@newValVar varchar(200),@tbfldid int,
 @paramvar varchar(50),@isString int,@fldID int,@isDetailTable int

set @dealtDS=''  --已经处理的数据源
select @defParams='',@callParams='',@procname='',@localvar=''


--procname
--存储过程名称规则：sys_sql_getfldval_{@parseid}_{@pretbname} ,@pretbname 为空，以"b"代替
select @procname='sys_sql_getfldval_' + convert(varchar(20),@parseid) + '_'
if (@predsname is null)
select @procname=@procname + 'b'
else
select @procname=@procname+@predsname

--1 存储过程固定输入参数
if (@recursLevel=0)
begin           -- 
--1.1 固定参数
select @callParams='@thisval,@useruid,@dpid,@cpid,@hasUpDS,@sys_sql_isUpBizExisted output ',
@defParams= '@thisval int ' + char(13) +
',@useruid int ' +char(13)+
',@dpid int'+char(13) +
',@cpid  int' +
',@hasUpDS int output' + char(13) + 
',@sys_sql_isUpBizExisted int output' + char(13)

--1.2 web参数
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  --web begin
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
 --调用参数列表
select @callParams=@callParams +',' + @paramvar
 
select @isString=0
--整数
set @i=charindex('int',@rdatatype)
if (@i>0)
begin
select @paramvar = @paramvar + ' int' + char(13)
end
set @i=charindex('date',@rdatatype)
if (@i>0)
begin
select @paramvar = @paramvar + ' datetime' + char(13)
end
set @i=charindex('decimal',@rdatatype)
if (@i>0)
begin
select @s=convert(varchar(2),@rprec)
if (@s is null)
set @s='2'
select @paramvar = @paramvar + ' decimal(18,' + @s+ ')' + char(13)
end
set @i=charindex('string',@rdatatype)
if (@i>0)
begin
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)
end

--定义参数
select @defParams=@defParams + char(13)  +',' + @paramvar

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

--1.3 前级数据源传入参数（predsname 参数）: 当前数据源记录ID
if (@predsname is not null and len(@predsname)>0)
begin
Declare curDS Cursor Static For
select distinct dsname from flatDSS
where parseid=@parseid and predsname=@predsname
order by dsname
Open curDS
Fetch next from curDS into @dsname
While (@@Fetch_status=0)
begin
--前级数据源主键字段名称，记录id
select @callParams = @callParams + ',@'+ @dsname + '_keyval'
select @defParams=@defParams + char(13)  +
',@' + @dsname+'_keyval int ' +char(13)

Fetch next from curDS into @dsname
end
Close curDS
Deallocate curDS
end
end -- 1 

--2 生成局部变量：数据源连接字段
if (@predsname is not null)
Declare dsCursor Cursor local static For
select distinct dsname,biztype,preLnkFld,keyfld,prekeyfld,shortname
from flatDSS
where parseid=@parseid and predsname=@predsname
order by dsname
else
Declare dsCursor Cursor local static For
select distinct dsname,biztype,preLnkFld,keyfld,prekeyfld,shortname
from flatDSS
where parseid=@parseid and (predsname is null or len(predsname)=0)
order by dsname



Open dsCursor
Fetch next From dsCursor Into  @dsname,@biztype,@preLnkFld,@keyfld,@prekeyfld,@shortname
While (@@Fetch_status=0)
BEGIN
--后续数据源，每级的字段个数，类型是一致的，逻辑字段名称也一致
--物理字段名称可能不一致，但可以强制物理名称也必须一致。
set @i=charindex(@dsname,@dealtDS)
if (@i>0)    --已经生成了的数据源，不处理
goto nxt

select @dealtDS = @dealtDS + ',' + @dsname

--数据源固定局部变量
select @localvar = @localvar +  '@' + @dsname + '_keyfld varchar(20),@' + @dsname + '_prelnkfld varchar(20)' + char(13)
select @localvar = @localvar +  ',@' + @dsname + '_biztype varchar(20)'
select @localvar = @localvar +  ',@' + @dsname + '_bizid int'
select @localvar = @localvar +  ',@' + @dsname + '_tagid int'
select @localvar = @localvar +  ',@' + @dsname + '_keyid int'+char(13)

if (@recursLevel>0)
select @localvar = @localvar +  ',@' + @dsname + '_keyval int'+char(13)


if (charIndex('BIZ',@biztype)>=1) --业务单数据源
begin
select @localvar = @localvar +  ',@' + @dsname + '_fbiztye varchar(10)'
select @localvar = @localvar +  ',@' + @dsname + '_fbizid int'
select @localvar = @localvar +  ',@' + @dsname + '_fordid int'
select @localvar = @localvar +  ',@' + @dsname + '_ford2tag int'
select @localvar = @localvar +  ',@' + @dsname + '_ford2id int'+char(13)

select @localvar = @localvar +  ',@' + @dsname + '_rbiztye varchar(10)'
select @localvar = @localvar +  ',@' + @dsname + '_rbizid int'
select @localvar = @localvar +  ',@' + @dsname + '_rordid int'
select @localvar = @localvar +  ',@' + @dsname + '_rord2tag int'
select @localvar = @localvar +  ',@' + @dsname + '_rord2id int'+char(13)
end  -- 业务单数据源 


--输出参数生成
--if (@recursLevel!=0)
Declare fldCursor Cursor local static For
select  distinct tbfld,datatype,fldsize,fldprec
from flatFld
where parseid=@parseid and dsname=@dsname
order by tbfld,datatype,fldsize,fldprec


Open fldCursor
Fetch next From fldCursor Into  @tbfld,@datatype,@fldsize,@fldprec
while (@@Fetch_status=0)
begin

exec sys_sql_genfldvar_one @dsname,@tbfld,@datatype,@fldsize,@fldprec,@fldDef  output,@fldDef_N  output
if (@fldDef is not null)
begin
if (@callParams='')
select @callParams = '@'+@dsname+'_' + @tbfld + ' output'
else
select @callParams = @callParams + ',@'+@dsname+'_' + @tbfld + ' output'

--生成过程定义参数
if (@defParams='')
select @defParams = '' + @fldDef + ' output'
else
select @defParams = @defParams + char(13) + ',' + @fldDef + ' output '
end

Fetch next From fldCursor Into  @tbfld,@datatype,@fldsize,@fldprec
end

Close fldCursor
Deallocate fldCursor

nxt:

--递归下级数据源参数
select @recursLevel = @recursLevel + 1

exec sys_sql_genparams @parseid, @dsname,@recursLevel, @nextDef output,@nextCall output,@nextProc output,@nextlocal output
--合并
if (@nextDef is not null and @nextDef != '')
begin
if (@DefParams = '')
select @DefParams =  @nextDef
else
select @DefParams = @DefParams + char(13) +','+@nextDef
end
if (@nextCall is not null and @nextCall != '')
begin
if (@callParams = '')
select @callParams =  @nextCall
else
select @callParams = @callParams + ','+@nextCall
end

if (@nextlocal is not null and @nextlocal != '')
begin
if (@localvar = '')
select @localvar =  @nextlocal
else
select @localvar = @localvar + ','+@nextlocal
end

Fetch next From dsCursor Into  @dsname,@biztype,@preLnkFld,@keyfld,@prekeyfld,@shortname

END


if (@recursLevel=0)
select @localvar=@localvar + char(13)+
'        ,@prebizType varchar(20) ,@prebizid int  ,@pretagid int,@preord_id int,@preord2_id int ' + char(13) +
'        ,@curord_id int,@curord2_id int ' 

Close dsCursor
Deallocate dsCursor

end