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

create procedure [dbo].[Sys_sql_expandFld](
@cmpid int,
@parseid int,
@errmsg varchar(2000) output 
)
as
begin
select @errmsg=''
--先删除
delete flatFld
where parseid=@parseid
 
insert into flatFld(parseid,dsname,fldname,fldexp,tbname,biztype,bizid,tagid,row,col)
select @parseid,a.dsname,b.fldname,b.fldexp,a.tbname,a.biztype,a.bizid,a.tagid,b.row,b.col
from flatDSS a,sys_sql_fld b
where a.parseid=b.parseid and a.dsname=b.dsname and a.parseid=@parseid 
--and b.fldexp not like '@%'

Declare @dsname varchar(20),@fldexp varchar(40), @biztype varchar(20),@bizid int,@tagid int,@tbfldid int,
 @rfldname varchar(30),@rdatatype varchar(30),@rsize int ,@rprec int,@fldid int,@row int,@col int,
 @fldDef varchar(50),@fldDef_N varchar(50),@tbname varchar(20),@isrealFld int
 
Declare fcursor Cursor Local Static For
select fldid,biztype,bizid,tagid,fldexp,dsname,row,col,tbname
from flatFld 
where parseid=@parseid

open fcursor
Fetch next From fcursor into @fldid,@biztype,@bizid,@tagid,@fldexp,@dsname,@row,@col,@tbname
While @@Fetch_Status = 0
Begin
--@cmpid int, @biztype  @bizid  @tagid  @srcname 
 --@rfldid int output, @rfldname output,@rdatatype @rsize  @rprec 
 
exec sys_sql_getFldInfo @cmpid , @biztype,  @bizid ,@tagid,@fldexp,@tbfldid output,
 @rfldname  output,@rdatatype  output,@rsize  output, @rprec  output
set @isrealFld=1
if (@rfldname is null or len(@rfldname)<1)
begin
if (charIndex('@',@fldexp)=0) 
begin
select @errmsg='行:' + convert(varchar(5),@row) + ' 列:' + convert(varchar(8),@col) + @tbname +'.' + @fldexp + ' 字段不存在！ '
return
end
select  @rfldname=@rfldname,@tbfldid=0,@rdatatype='',@rsize=0,@rprec=0,@isrealFld=0

end

if (@rfldname is  not null)
begin
  update flatFld
   set tbfld=@rfldname,
 tbfldid=@tbfldid,
 fldvar='@' + @dsname + '_' + @rfldname + '__',
 fldvarnv='@' + @dsname + '_' + @rfldname+'_nv__',
 datatype=@rdatatype,
 fldsize=@rsize,
 fldprec = @rprec
  where fldid=@fldid 
end
-- 用户数据变量生成
if (@isrealFld=1)
exec sys_sql_genfldvar_one @tbname,@rfldname,@rdatatype,@rsize,@rprec,0,@fldDef output,@fldDef_N output

Fetch next From fcursor into @fldid,@biztype,@bizid,@tagid,@fldexp,@dsname,@row,@col,@tbname 

End
close fcursor
deallocate fcursor

END