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

--求指定表相关字段列表
create procedure [dbo].[sys_sql_get_flds](
@parseid int, 
@tbname varchar(20),--物理表名
@forceRedo int,
@flds varchar(1000) output,--相关字段列表（用于SELECT）
@fldvars varchar(4000) output,--每个字段对应的变量列表（用于Into cursor）
@fldvals varchar(4000) output--字段变量=字段,列表（用于取值）
) 
as
begin
declare @fldDefs varchar(4000)       --变量定义
select @flds = flds,@fldvars=fldvars,@fldvals=fldvals,@fldDefs=fldDefs
from flatDSVars
where tbname=@tbname
if (@forceRedo=0 and @@rowcount>0)
begin 
if (@flds is null)
select @flds='',@fldvars='',@fldvals='',@fldDefs=''
return
end
if (@tbname is null)
set @tbname='NULL' 

--展开本数据源涉及的详细详细
Declare @tbfld varchar(20), @fldvar varchar(20),@datatype varchar(20),@fldsize int,@fldprec int,@i int,
@s varchar(2000), @predsname varchar(20),@err varchar(4000),@dsname varchar(20),
@prelnkfld varchar(20),@fldid int,@biztype varchar(10),@tagid int,@isupper int,@isroot int,@isdetailtable int,
@nDsname varchar(20),@nBiztype varchar(10),@nBizID int,@nTagid int,@nisupper int,@nisroot int, @nisdetailTable int,
@lastDS varchar(20),@nprelnkfld varchar(20),@flddefinition varchar(1000) ,@keyfld varchar(20),@nkeyfld varchar(20),
@fldDef varchar(50),@fldDef_N varchar(50),@pretbname varchar(20)

select @flds = '',@fldvars='',@fldvals='',@fldDefs=''

declare @defedFld table (fldname varchar(20))

--当前数据源名称
select @dsname=dsname,@biztype=biztype,@tagid=tagid,@isupper=isupper,@isroot=isroot,
@isdetailtable=isdetailtable,@keyfld=keyfld
from flatDSS
where tbname=@tbname

  --后继数据源信息
Declare nds cursor static For
select dsname,biztype,tagid,isupper,isroot,isdetailtable,prelnkfld
from flatDSS
where pretbname=@tbname
order by dsname

exec sys_sql_genfldvar_one @tbname,@keyfld,'int',0,0,1,@fldDef ,@fldDef_N 

Open nds
Fetch next From nds into @ndsname,@nbiztype,@ntagid,@nisupper,@nisroot,@nisdetailtable,@nprelnkfld
declare @cnt int
select  @cnt=0,@lastDs=''

-- 连接字段数据
While (@@fetch_status = 0)
Begin
 
--根据后继数据源类型，定义变量，取值，游标变量
--1 后继 : 上级
if (@nisupper=1) 
begin
if (@tagid=0)  --1.1 主表
begin
--字段
exec sys_sql_genfldvar_one @tbname,'ord_ftype','int',0,0,1,@fldDef ,@fldDef_N 
exec sys_sql_genfldvar_one @tbname,'ord_ford2tag','int',0,0,1,@fldDef ,@fldDef_N 
exec sys_sql_genfldvar_one @tbname,'ord_fbizid','int',0,0,1,@fldDef ,@fldDef_N 
exec sys_sql_genfldvar_one @tbname,'ord_fordid','int',0,0,1,@fldDef ,@fldDef_N 
exec sys_sql_genfldvar_one @tbname,'ord_ford2id','int',0,0,1,@fldDef ,@fldDef_N 
end  ---主表

if (@tagid=1)  --1.2 明细表
begin
--字段
exec sys_sql_genfldvar_one @tbname,'ord2_ftype','int',0,0,1,@fldDef ,@fldDef_N 
exec sys_sql_genfldvar_one @tbname,'ord2_ford2tag','int',0,0,1,@fldDef ,@fldDef_N 
exec sys_sql_genfldvar_one @tbname,'ord2_fbizid','int',0,0,1,@fldDef ,@fldDef_N 
exec sys_sql_genfldvar_one @tbname,'ord2_fordid','int',0,0,1,@fldDef ,@fldDef_N 
exec sys_sql_genfldvar_one @tbname,'ord2_ford2id','int',0,0,1,@fldDef ,@fldDef_N 
end  ---主表
end --1 后继 : 上级

--2 后继 : 根
if (@nisroot=1) 
begin
if (@tagid=0)  --1.1 主表
begin
exec sys_sql_genfldvar_one @tbname,'ord_rtype','int',0,0,1,@fldDef ,@fldDef_N 
exec sys_sql_genfldvar_one @tbname,'ord_rord2tag','int',0,0,1,@fldDef ,@fldDef_N 
exec sys_sql_genfldvar_one @tbname,'ord_rbizid','int',0,0,1,@fldDef ,@fldDef_N 
exec sys_sql_genfldvar_one @tbname,'ord_rordid','int',0,0,1,@fldDef ,@fldDef_N 
exec sys_sql_genfldvar_one @tbname,'ord_rord2id','int',0,0,1,@fldDef ,@fldDef_N 
end  ---主表
if (@tagid=1)  --2.2 明细表
begin
exec sys_sql_genfldvar_one @tbname,'ord2_rtype','int',0,0,1,@fldDef ,@fldDef_N 
exec sys_sql_genfldvar_one @tbname,'ord2_rord2tag','int',0,0,1,@fldDef ,@fldDef_N 
exec sys_sql_genfldvar_one @tbname,'ord2_rbizid','int',0,0,1,@fldDef ,@fldDef_N 
exec sys_sql_genfldvar_one @tbname,'ord2_rordid','int',0,0,1,@fldDef ,@fldDef_N 
exec sys_sql_genfldvar_one @tbname,'ord2_rord2id','int',0,0,1,@fldDef ,@fldDef_N 
end  ---
 end
    
    --后继：普通数据源
    if (@nisupper=0 and @nisroot=0 and @nisdetailtable=0)
    begin
--字段
exec sys_sql_genfldvar_one @tbname,@nprelnkfld,'int',0,0,1,@fldDef ,@fldDef_N   
    end 
    
select @cnt=@cnt+1 
nxt:
Fetch next From nds into @ndsname,@nbiztype,@ntagid,@nisupper,@nisroot,@nisdetailtable,@nprelnkfld
End
close nds 
deallocate nds
   
end