IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[sys_sql_addUptable]') AND type = 'P')
DROP PROCEDURE [dbo].[sys_sql_addUptable]
GO
--procedure sys_sql_addUptable @cmpid,@biztype,@bizid,@tagid,@ord_id,@ord2_id
-- 建立新的上级关系
create procedure [dbo].[sys_sql_addUptable](
@cmpid int,
@biztype varchar(10),
@bizidint,
@tagid int,--@tagid 为0，为主表
@ord_id int,-- 链式更新用
@ord2_id int-- 链式更新用
)
as
begin

Declare @tableName varchar(20),@keyFld varchar(20),@upTableName varchar(20),@upKeyFld varchar(20),
@existed int,@upbiztype varchar(10),@upbizid int,@uptagid int, @upordid int,@upord2id int

select @upbiztype='BIZ'
-- 获得数据表信息@cmpid ,@biztype ,@bizid ,@tagid ,@tablename  output,@pkfld  output
exec sys_sql_getTableNamePk @cmpid,@biztype,@bizid,@tagid,@tableName output,@keyFld output

declare @sql nvarchar(2000) 

-- 1上级
-- 取连接上级记录ID值
if (@tagid=0)
select @sql='select @upbizid=ord_fbizid, @uptagid=ord_ford2tag,  @upordid=ord_fordid,@upord2id = ord_ford2id' +
' from ' + @tableName  +
' where ord_id= ' + convert(varchar(15),@ord_id) 
else
select @sql='select @upbizid=ord2_fbizid, @uptagid=ord2_ford2tag,  @upordid=ord2_fordid,@upord2id= ord2_ford2id' +
' from ' + @tableName  +
' where ord2_id= ' + convert(varchar(15),@ord2_id) 

select @upbizid=0,@upordid=0,@upord2id=0

exec sp_executesql @sql, N'@upbizid int out,@uptagid int out,@upordid int out,@upord2id int out ',
@upbizid out, @uptagid out,@upordid out,@upord2id out

if ((@upordid is not null or @upord2id is not null) and (@upordid>0 or @upord2id >0))
begin
exec sys_sql_getTableNamePk @cmpid,@upbiztype,@upbizid,@uptagid, @upTableName output,@upKeyFld output

select @existed=count(*)
from sys_sql_uptables
where cmpid=@cmpid and tablename=@tableName and uptablename=@uptablename

if (@upbizid is null)
set @upbizid=0
if (@uptagid is null)
set @uptagid=0
if (@upbiztype is null)
set @upbiztype='BIZ'

if (@existed=0 and @uptablename is not null and @upkeyfld is not null)
insert into sys_sql_uptables(cmpid,biztype,bizid,tagid,tablename,keyfld,upbiztype,upbizid,uptagid,uptablename,upkeyfld)
values(@cmpid,@biztype,@bizid,@tagid,@tablename,@keyfld,@upbiztype,@upbizid,@uptagid,@uptablename,@upkeyfld)

--递归,上级的上级
exec sys_sql_addUptable @cmpid,@upbiztype,@upbizid,@uptagid,@upordid,@upord2id
end

-- 2 根
if (@tagid=0)
select @sql='select @upbizid=ord_rbizid, @uptagid=ord_rord2tag,  @upordid=ord_rordid,@upord2id = ord_rord2id' +
' from ' + @tableName  +
' where ord_id= ' + convert(varchar(15),@ord_id) 
else
select @sql='select @upbizid=ord2_rbizid, @uptagid=ord2_rord2tag,  @upordid=ord2_rordid,@upord2id = ord2_rord2id' +
' from ' + @tableName  +
' where ord2_id= ' + convert(varchar(15),@ord2_id) 

select @upbizid=0,@upordid=0,@upord2id=0

exec sp_executesql @sql, N'@upbizid int out,@uptagid int out,@upordid int out,@upord2id int out ',
@upbizid out, @uptagid out,@upordid out,@upord2id out

if ((@upordid is not null or @upord2id is not null) and (@upordid>0 or @upord2id >0))
begin

exec sys_sql_getTableNamePk @cmpid,@upbiztype,@upbizid,@uptagid, @upTableName output,@upKeyFld output

select @existed=count(*)
from sys_sql_uptables
where cmpid=@cmpid and tablename=@tableName and uptablename=@uptablename

if (@existed=0 and @uptablename is not null and @upkeyfld is not null)
insert into sys_sql_uptables(cmpid,biztype,bizid,tagid,tablename,keyfld,upbiztype,upbizid,uptagid,uptablename,upkeyfld)
values(@cmpid,@biztype,@bizid,@tagid,@tablename,@keyfld,@upbiztype,@upbizid,@uptagid,@uptablename,@upkeyfld)


--递归,上级的上级
exec sys_sql_addUptable @cmpid,@upbiztype,@upbizid,@uptagid,@upordid,@upord2id
end

return
end