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

create procedure [dbo].[sys_sql_getfldval](
@cmpid int,
@parseid int,
@dsname varchar(20),
@tbname varchar(30),
@keyfld varchar(20),
@blanks varchar(50),--前导空格
@script varchar(1000) out
)
as
begin
if @blanks is null
set @blanks = '  '

if (@tbname is null)
set @tbname='NULL'

select @script=char(13) + @blanks + ' -- 取字段值:' + @tbname + ' ds:' + @dsname + char(13)

--展开本数据源涉及的详细详细
Declare @i int,@s varchar(2000), @fldvals varchar(1000),@varValP varchar(4000),@predsname varchar(20),@err varchar(4000),
@prelnkfld varchar(20),@targetDS varchar(20),@fldid int,@biztype varchar(10),@tagid int,@isDetailTable int,@isDetail2 int,
@lnkpart varchar(200),@lnkval varchar(200), @startInd int,@varType varchar(10) ,@varName varchar(20),@frmInd int,@toInd int,
@thePredsname varchar(20),@pretbname varchar(20),@idParamVar varchar(20),@nullvals varchar(2000),
@preIsDetail int,@prebiztype varchar(10),@prebizid int,@pretagid int,@tbdatabase varchar(30)

select @fldvals = '' ,@nullvals=''

select @biztype=biztype,@tagid=tagid,@targetDS = @dsname,@isDetailTable=isDetailTable,@prelnkfld=preLnkFld, 
        @thePredsname=predsname,@keyfld=keyfld,@pretbname=pretbname,@idParamVar=idParamVar,@tbdatabase=tbdatabase
from flatDSS
where tbname=@tbname

if (@tbdatabase is null)
set @tbdatabase=''
--连接字段赋值
if (@idParamVar is not null and len(@idParamVar)>1)
select @script=@script + @blanks + 'select @'+@tbname+'_'+@keyfld+'__=' + @idParamVar +char(13)
else if (@prelnkfld is not null and len(@prelnkfld)>1)
select @script=@script + @blanks + 'select @'+@tbname+'_'+@keyfld+'__=@' + @pretbname + '_' + @preLnkFld+'__' +char(13)

 
--取连接关系
select @lnkpart=lnkpart
from sys_sql_cond
where parseid=@parseid and lnkds like '%'+@dsname+'%'

--字段赋值列表
select @fldvals=fldvals,@nullvals=nullvals
from flatDSVars
where tbname=@tbname

--求表达式值
set @lnkval=null
if (@lnkpart is not null)
exec sys_sql_replaceExp @parseid,@lnkpart,@lnkval output


if (len(@fldvals)>2)
begin
select @script = @script + 
 @blanks  + ' select ' + @fldvals + char(13) +
 @blanks  + '    from ' + @tbdatabase + @tbname + char(13) 
--连接关系
-- 1 确定连接关系
if (@isDetailTable=0) --非明细表
select @script = @script + 
 @blanks  + '    where ' + @keyfld + '=@'+@tbname +  '_' + @keyfld + '__'
else   -- 明细表
begin
select @script = @script + 
 @blanks  + '    where ord2_ordid=@'+@pretbname + '_' + @keyfld
--用户添加链接关系 
if ( @lnkval is not null and  len(@lnkval)>2)
select @script = @script + ' and ' + @lnkval + char(13)+char(13)
end 

if (len(@nullvals)>2)
begin
select @script = @script + 
 @blanks  + '   -- 如果取到到值，设置为缺省值 ' + char(13) +
 @blanks  + '   if (@@rowcount=0) ' + char(13) +
 @blanks  + '   select ' +  @nullvals + char(13) +
 @blanks  + '   set @' + @tbname +'_rowcount__=@@rowcount ' + char(13) 
end
select @script = @script + char(13) 
end
else
select @script = @script +   
 @blanks  + ' -- 不需要取值：' +  @tbname +  char(13) 
 
update flatDSS
set isLoaded=1
 where tbname=@tbname


if (@script is null)
set @script=''

end