IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[sys_sql_genfldvar_one]') AND type = 'P')
DROP PROCEDURE [dbo].[sys_sql_genfldvar_one]
GO
--exec sys_sql_genfldvar_one @tbname,@tbfld,@fdatatype,@fsize,@fprec,@fldDef output,@fldDef_N output
 
create procedure [dbo].[sys_sql_genfldvar_one](
@tbname varchar(20),
@tbfld varchar(20),
@fdatatype varchar(20), 
@fsize int,  
@fprec int,
@isSys int,
@fldDef varchar(50)  output,
@fldDef_N varchar(50) output
) 
as
begin
Declare @i int,@s varchar(200),@fldvar_N varchar(50), @fldvar varchar(2000),@fldval varchar(2000),@dsname varchar(20),@nullval varchar(2000)
 ,@iskeyfld int,@isNew int
 
--判断是否主键：对于非明细表，取数时不取主键值
set @iskeyfld=0
if (exists(select * from flatDSS where tbname=@tbname and keyfld=@tbfld))
set @iskeyfld=1

 select @fldvar='@' + @tbname + '_' + @tbfld + '__'  --变量名称 
select @fldDef_N = @fldvar + '_nv__' ,@fldDef=@fldvar,@fldval=@fldvar + '=' + @tbfld,@fldvar_N=@fldvar+'_nv__'
 
exec sys_sql_addvar @fldvar,@fdatatype,@fsize,@fprec,@isNew output
exec sys_sql_addvar @fldDef_N,@fdatatype,@fsize,@fprec,@isNew output

set @i=charindex('char',@fdatatype)
if (@i>0)
select @fdatatype='string'

if (@fdatatype is null)
set @fdatatype='string'
if (@fsize is null)
set @fsize=20
if (@fprec is null)
set @fprec=2
 
--整数
set @i=charindex('int',@fdatatype)
if (@i>0)
begin
select @fldDef=@fldDef + ' int'
select @fldDef_N = @fldDef_N + ' int ' ,@nullval=@fldvar+'=0'
end
set @i=charindex('date',@fdatatype)
if (@i>0)
begin
select @fldDef=@fldDef +' datetime'
select @fldDef_N = @fldDef_N + ' datetime ',@nullval=@fldvar+'=getdate()'
end
set @i=charindex('decimal',@fdatatype)
if (@i>0)
begin
select @s=convert(varchar(2),@fprec)
if (@s is null)
set @s='2'
select @fldDef=@fldDef +' decimal(18,' + @s +')'
select @fldDef_N = @fldDef_N + ' decimal(18,' + @s+ ')',@nullval=@fldvar+'=0'
end
set @i=charindex('string',@fdatatype)
if (@i>0)
begin
if (@fsize is null or @fsize<2)
set @fsize=50

select @s=convert(varchar(4),@fsize)
if (@s is null)
set @s='50'
select @fldDef=@fldDef + ' varchar(' + @s+ ')'
select @fldDef_N = @fldDef_N + ' varchar(' + @s+ ')' ,@nullval=@fldvar+'='''''
end

if (@isSys=1)
select @fldvar_N='',@fldDef_N=''
 
  update flatFld
   set fldvar=@fldvar,
 fldvarnv=@fldvar_N,
 datatype=@fdatatype,
 fldsize=@fsize,
 fldprec = @fprec,
 fldDefinition=@fldDef, 
 fldDefNV=@fldDef_N 
where tbname=@tbname and tbfld=@tbfld

--加入临时表
select @dsname=dsname
from flatDSS
where tbname=@tbname
 
declare @flds varchar(2000),@fldvars varchar(2000),@fldvals varchar(2000),@fldDefs varchar(2000)
         ,@nullvals varchar(2000)

set @isnew = 0
select @flds=flds,@fldvars=fldvars,@fldvals=fldvals,@fldDefs=fldDefs,@nullvals=nullvals
from flatDSVars 
where tbname=@tbname
 
if (@@rowcount=0)
select @isnew=1,@flds='',@fldvars='',@fldvals='',@fldDefs='',@nullvals=''
--判断是否已经定义
set @i=charindex(@fldDef,@fldDefs)
if (@i=0)
begin
if (@flds='')
begin
if (@isSys=1)
if (@iskeyfld=0)
select @flds=@tbfld,@fldvars=@fldvar,@fldvals=@fldval,@fldDefs=@fldDef,@nullvals=@nullval
else
select @flds=@tbfld,@fldvars=@fldvar,@fldDefs=@fldDef,@nullvals=@nullval
else
if (@iskeyfld=0)
select @flds=@tbfld,@fldvars=@fldvar,@fldvals=@fldval,@nullvals=@nullval,@fldDefs=@fldDef + ',' + @fldDef_N
else
select @flds=@tbfld,@fldvars=@fldvar,@nullvals=@nullval,@fldDefs=@fldDef + ',' + @fldDef_N
end
else
begin
if (@isSys=1)
if (@iskeyfld=0)
select @flds=@flds+ ',' + @tbfld,@fldvars=@fldvars + ',' +@fldvar,@nullvals=@nullvals + ',' +@nullval,
@fldvals=@fldvals + ',' + @fldval,@fldDefs=@fldDefs + ',' + @fldDef 
else
select @flds=@flds+ ',' + @tbfld,@fldvars=@fldvars + ',' +@fldvar,@nullvals=@nullvals + ',' +@nullval,
@fldDefs=@fldDefs + ',' + @fldDef 
else
if (@iskeyfld=0)
select @flds=@flds+ ',' + @tbfld,@fldvars=@fldvars + ',' +@fldvar,@nullvals=@nullvals + ',' +@nullval,
@fldvals=@fldvals + ',' + @fldval,@fldDefs=@fldDefs + ',' + @fldDef + ',' + @fldDef_N
else
select @flds=@flds+ ',' + @tbfld,@fldvars=@fldvars + ',' +@fldvar,@nullvals=@nullvals + ',' +@nullval,
@fldDefs=@fldDefs + ',' + @fldDef + ',' + @fldDef_N
end 

set @i=charindex(',',@fldvals)
if (@i<3)
begin
select @i=@i+1
select @fldvals =substring(@fldvals,@i,len(@fldvals))
end

--保存
if (@isnew=1)
insert into flatDSVars(dsname,tbname,flds,fldvars,fldvals,fldDefs,nullvals)
values(@dsname,@tbname,@flds,@fldvars,@fldvals,@fldDefs,@nullvals)
else 
update flatDSVars
set flds=@flds,fldvars=@fldvars,fldvals=@fldvals,fldDefs=@fldDefs,nullvals=@nullvals
where tbname=@tbname
end

end