IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[sys_sql_gettable_up]') AND type = 'P')
DROP PROCEDURE [dbo].[sys_sql_gettable_up]
GO
-- 定义运行时变量: 主程序定义
--select @script=@script + char(13) +
--' Declare @tables varchar(100),@links varchar(200) --连接表，连接关系 ' + char(13)

-- 定义运行时变量: 主程序定义
--select @script=@script + char(13) +
--' Declare @tables varchar(100),@links varchar(200) --连接表，连接关系 ' + char(13)

create procedure [dbo].[sys_sql_gettable_up] (
@dschain varchar(50),
@pretbname varchar(20),
@script varchar(1000) output
)
as 
begin

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
set @script = ''
-- 0 数据源处理：取头数据源
declare @dsname varchar(10),@toInd int,@i int
exec sys_sql_getNextSysWord @dschain,1,@dsname,@toInd out
select @dsname=rtrim(ltrim(substring(@dschain, 1,@toInd-1))),@dschain=substring(@dschain,@toInd+1,200)
-- 1 
if (@pretbname is null)
Declare todealds Cursor local static For
select tbname,keyval,preLnkFld,pretbname,prekeyfld,shortname,keyfld,bizid,biztype
from #flatdss
where pretbname is null
else
Declare todealds Cursor local static For
select tbname,keyval,preLnkFld,pretbname,prekeyfld,shortname,keyfld,bizid,biztype
from #flatdss
where pretbname = @pretbname

--2,3 从todealds中取首记录，如果没有记录，结束
Declare @tbname varchar(20),@keyval varchar(20),@preLnkFld varchar(20),@biztype varchar(20),
@keyfld varchar(20),@prekeyfld varchar(20),@shortname varchar(20),@bizid int,@preLnkVal varchar(20)

Open todealds
Fetch Next From todealds Into @tbname ,@keyval,@preLnkFld ,@pretbname,@prekeyfld,
@shortname,@keyfld,@bizid,@biztype
if @@FETCH_STATUS != 0
goto over1



-- 4 生成与前级的连接关系
--  4.1 
if (@shortname != '上级')
begin
-- 4.1.1 连接
select @script=@script + char(13) + 
'  if @tables__ !=''''  ' + char(13) +
'  begin ' + char(13) +
'    select @tables__ = @tables__ , ' +  @tbname + char(13)   --连接表列表

if @pretbname is not null
  --表连接关系
select @script=@script + char(13) + 
'    if @links__ = '''' ' + char(13)+
'      select @links__=' +  @pretbname + '.' + @preLnkFld + '=' + @tbname + '.' + @keyfld + char(13)+
'    else ' +char(13) +
'      select @links=@links and ' + char(13) +
'      ' + @pretbname + '.' + @preLnkFld + '=' + @tbname + '.' + @keyfld + char(13) 

select @script=@script + char(13) + 
'  end ' + char(13) +
'  else ' + char(13) +
'    select @tables=' + @tbname + char(13)

if (@keyval is not null) -- 有约束字段 
select @script=@script + char(13) + 
'  if @links = '''' ' + char(13) +
'    select @links=' +  @tbname + '.' + @keyfld + '=' + convert(varchar(12), @keyval) + char(13)+
'  else ' +char(13) +
'    select @links=@links and ' + char(13) +
'      ' + @tbname + '.' + @keyfld + '=' + convert(varchar(12), @keyval) + char(13)
-- 4.1.2 不要

-- 4.1.3 递归
declare @script2 varchar(1000)
exec sys_sql_gettable @dschain,@tbname,@script2 output

--4.1.4 合并脚本
select @script = @script + char(13) + @script2
goto over1
end -- 4.1

--4.2  下面处理 “上级” 数据源
-- 4.2.1 获取连接信息
select @script=@script + char(13) + 
'  select @upbiztype = ''BIZ'' ,@upbizid=ord_fbizid,@prelnkval=ord_fordid ' +char(13) +
'    from ' +  @pretbname + char(13) +
'      where ord_id=@prekeyval' + char(13)

--4.2.2 循环
While (@@FETCH_STATUS = 0)
begin
select @script=@script + char(13) + 
'  if @upbizid=' + convert(varchar(4), @bizid) + char(13) +
'  begin ' + char(13) 

-- 4.2.2.1 连接
select @script=@script + char(13) + 
'    if @tables !=''''  ' + char(13) +
'    begin ' + char(13) +
'      select @tables = @tables , ' +  @tbname + char(13)   --连接表列表

if @pretbname is not null  --表连接关系
select @script=@script + char(13) + 
'    if @links = '''' ' + char(13)+
'      select @links=' +  @pretbname + '.' + @preLnkFld + '=' + @tbname + '.' + @keyfld + char(13)+
'    else ' +char(13) +
'      select @links=@links and ' + char(13) +
'           ' + @pretbname + '.' + @preLnkFld + '=' + @tbname + '.' + @keyfld + char(13) 

select @script=@script + char(13) + 
'    end ' + char(13) +
'    else ' + char(13) +
'      select @tables=' + @tbname + char(13)

if (@keyval is not null) -- 有约束字段 
select @script=@script + char(13) + 
'    if @links = '''' ' + char(13) +
'      select @links=' +  @tbname + '.' + @keyfld + '=' +convert(varchar(12), @keyval) + char(13)+
'    else ' +char(13) +
'      select @links=@links and ' + char(13) +
'          ' + @tbname + '.' + @keyfld + '=' + convert(varchar(12), @keyval) + char(13)
-- 4.2.2.2 连接

-- 4.2.2.3 连接
exec sys_sql_gettable @dschain,@tbname,@script2 output

--4.2.2.4 合并脚本
select @script = @script + char(13) + @script2 +
' end '


Fetch Next From todealds Into @tbname ,@keyval,@preLnkFld ,@pretbname,@prekeyfld,
@shortname,@keyfld,@bizid,@biztype
end

over1:
Close todealds
Deallocate todealds

end