哈 先谢谢各位的帮忙 祝中秋happy
问题:
Data_Merch(父子档案关系表)
FMerch SMerch
b c
c d
d a
=============
这是表中数据
现在要插入数据
比如(x,y)
插入前想判断是否出现死循环
也就是y的所有子档案里面(无限层),如果含有x,就是错误,不能插入
还有x的徐哦有父档案里面(无限层),如果含有y,就有错误,不能插入比如 插入 (a,b) 这个是错误的 因为 b->c->d->a 形成了死循环
插入(a,d),也是错误的不能准确表达,就这样描述一下
请问怎么实现比较好
用SQL语句如不行,借助代码 谢谢先~

解决方案 »

  1.   

    简单表述一下Data_Merch(父子档案关系表)
    FMerch SMerch
    b c
    c d
    d e
    e f
    e g
    d a
    ==============
    我要得到结果
    b c
    b d
    b e
    b f
    b g
    b a
      

  2.   

    我的代码不过不理想declare @i int
    set @i=1
    while @i<=100
    begin
    insert into #Temp select * from #T where F = 'a'
    insert into #Temp select * from #T where F in (select Z from #Temp)
    set @i = @i + 1
    endselect distinct z,* from #Temp我是把循环次数写死
    是在没辙
      

  3.   

    我觉得在表里加一个字段Layer表示层次,这样就很好解决了(父结点只能是子结点的上一层)
    ---------
    FMerch SMerch Layer
    0      b      1
    b      c      2
    c      d      3
    d      a      4
      

  4.   

    --检查父节点是否重复,可以参照写检查子节点是否重复
    CREATE PROCEDURE [getP]
    @Son nvarchar(50),
    @SourceParent  nvarchar(50)
     AS
    DECLARE @parent nvarchar(50)
    DECLARE @return_status intDECLARE testcursor CURSOR FOR
    select FMerch
     from Data_Merch
    where  SMerch = @SonOPEN testcursor FETCH NEXT FROM testcursor
    into  @parent
    WHILE @@FETCH_STATUS = 0
    BEGIN
       if @parent = @SourceParent
      begin
         return -1
      end
      exec @return_status = getP @parent
      if @return_status = -1
      begin
         return -1
      end
         FETCH NEXT FROM testcursor
    into  @parent
    ENDCLOSE testcursor
    DEALLOCATE testcursor
    return 0
      

  5.   

    使用:
    比如 插入 (a,b)前
    exec @return_status = getP 'b','a'  
      if @return_status = -1
      begin
         --父节点有重复
      end
    注:
    上一贴存储过程中有笔误 
    exec @return_status = getP @parent改为
    exec @return_status = getP @parent,@SourceParent
      

  6.   

    谢谢 我看看
    Data_Merch(父子档案关系表)
    FMerch SMerch
    b c
    c d
    d e
    e f
    e g
    d a
    ==============
    我要得到结果
    b c 
    b d
    b e
    b f
    b g
    b a
      

  7.   

    -----临时表的写法
    declare @P nvarchar(50)
    declare @S nvarchar(50)
    set @P = 'a'
    set @S = 'b
    '
    declare @i int
    declare @J int
    create #T
    {
    a vchar(50),
    }insert #T value(@P)
    set @i =1
    while @i <> 0
    begin
      select FMerch  into #T
      from Data_Merch where  SMerch in (select a from #T)
      select @i = count(*) from  #T
      select @J = count(*) from  #T where a = @S
      if @J<> 0
      begin
        return -1 --有重复的FMerch
      end
    endinsert #T value(@S)
    set @i =1
    while @i <> 0
    begin
      select SMerch  into #T
      from Data_Merch where  FMerch in (select a from #T)
      select @i = count(*) from  #T
      select @J = count(*) from  #T where a = @P
      if @J<> 0
      begin
        return -2 --有重复的SMerch
      end
    end
      

  8.   

    谢谢 我看看
    Data_Merch(父子档案关系表)
    FMerch SMerch
    b c
    c d
    d e
    e f
    e g
    d a
    ==============
    我要得到结果
    b c 
    b d
    b e
    b f
    b g
    b a-----临时表的写法
    declare @P nvarchar(50)
    set @P = 'b'
    '
    declare @i int
    declare @J int
    create #T
    {
    P vchar(50),
    S vchar(50),
    Layer int
    }
    set @J = 1
    select SMerch as SMerch into #T1 from Data_Merch where FMerch = @P
    select @i = count(*) from  #T1
    insert into #T(s,P,Layer) select SMerch ,@P,@J from #T1
    while @i <> 0
    begin
      set @J = @J + 1
      select SMerch as SMerch into #T1 from Data_Merch where FMerch in (select SMerch from #t1)
      select @i = count(*) from  #T1
      insert into #T(s,P,Layer) select SMerch ,@P,@J from #T1
    end
    select * from #T