比如一個BOM表,我現在要進行此BOM表所有節點的拷貝
每個節點有BOM編號和自身的節點編號,以及上級節點編號
我的方法是每拷貝一個節點就將源節點的子節點拷貝並且把上級節點編號修改成剛拷貝生成的那個節點的編號,我在程序裡面處理的,效率不理想
所以想找人幫我寫個存儲過程,我自己不會,呵呵

解决方案 »

  1.   

    完了過來取分
    http://expert.csdn.net/Expert/topic/2182/2182552.xml?temp=.1866876
      

  2.   

    只拷贝一个节点,要拷贝所有节点要用光标create procedure spCopyBOM
    @id int
    asdeclare @newid intinsert into Table2 (BOMID, pID)
    select BOMID, NULL from Table1 where ID = @idSet @newid = @@IDENTITY insert into Table2 (BOMID, pID)
    select BOMID, @newid from Table1 where pID = @id
      

  3.   

    按照你這樣寫的話,我怎麼傳參數
    每復制一個節點(包涵子節點)就得調用一次存儲過程嘛
    我需要同時復制N個節點呀
    這樣效率是傳入一個類似輿"1,3,5,6,66"的ID集合字符串的方式的1/N
      

  4.   

    没看明白,你表的方式举个例,比如传入@str='1,3,5,6,66'
    传入前原表怎样的?
    id  pid
    .....
    .....传入后想生成怎样变化表?
    id  pid
    .....
    .....
      

  5.   

    比如
    id=10 的pID = 1
    id=11 的pID = 3
    id=12 的pID = 5
    id=13 的pID = 6
    id=14 的pID = 13
    id=15 的pID = 10
    那麼當傳入"1,3,5,6"後,需要構建一批新節點
    摹擬生成的新節點
    源節點ID      ID       pID
    1             20        0     (因為,1,3,5,6本來的pID=0)
    3             21        0
    5             22        0
    6             23        0
    10            24        20
    11            25        21
    12            26        22
    13            27        23
    14            28        27
    15            29        24
      

  6.   

    CREATE procedure spCopyBOM
    @idstr varchar(255) -- "1,3,5,6"
    asdeclare @idx1 int
    declare @idx2 int
    declare @ID int
    declare @newid intSet @idx1 = 1
    while charindex(',', @idstr, @idx1) > 0
    begin
    Set @idx2 = charindex(',', @idstr, @idx1)
    Set @ID = Cast(SubString(@idstr, @idx1, @idx2 - @idx1) as int) insert into Table2 (BOMID, pID)
    select BOMID, 0 from Table1 where ID = @ID Set @newid = @@IDENTITY  insert into Table2 (BOMID, pID)
    select BOMID, @newid from Table1 where pID = @ID Set @idx1 = @idx2 + 1
    endif @idx1 <= len(@idstr)
    begin
    Set @ID = Cast(SubString(@idstr, @idx1, len(@idstr) - @idx1 + 1) as int) insert into Table2 (BOMID, pID)
    select BOMID, 0 from Table1 where ID = @ID Set @newid = @@IDENTITY  insert into Table2 (BOMID, pID)
    select BOMID, @newid from Table1 where pID = @ID
    end
    GO
      

  7.   

    主要是你ID是个identity列,怎么处理效率都不会高。
    多用户情况下,还存在不确定性问题。
    最好把ID改非IDENTITY列。
      

  8.   

    源節點ID      ID       pID
    1             20        0     (因為,1,3,5,6本來的pID=0)
    3             21        0
    5             22        0
    6             23        0
    10            24        20
    11            25        21
    12            26        22
    13            27        23
    14            28        27
    15            29        24
    这个是元结果集还是目标结果集??你把两个都列出看看。
      

  9.   

    源節點ID      ID       pID
    1             20        0     (因為,1,3,5,6本來的pID=0)
    3             21        0
    5             22        0
    6             23        0
    10            24        20
    11            25        21
    12            26        22
    13            27        23
    14            28        27
    15            29        24
    这个是源结果集还是目标结果集??你把两个都列出看看。
      

  10.   

    这个不知道是否合适你
    /*--复制树形结点 将一个结点下的所有子结点复制到另一个结点下--*/
    --创建数据测试环境
    create table #tb(id int identity(1,1),parentid int,name varchar(10))
    insert into #tb
    select 0,'中国'
    union all select 1,'广东'
    union all select 1,'广西'
    union all select 1,'四川'
    union all select 2,'广州'
    union all select 2,'佛山'
    union all select 2,'东莞'
    union all select 5,'越秀区'
    union all select 5,'海珠区'
    union all select 5,'芳村'
    union all select 6,'禅城区'
    union all select 6,'南海区'
    union all select 11,'石湾'
    go--创建复制的存储过程
    create proc p_copy
    @s_id int, --复制该项下的所有子项
    @d_id int --复制到此项下
    as
    declare @nid int,@oid int,@name varchar(10)
    select id,name into #temp from #tb where parentid=@s_id
    while exists(select 1 from #temp)
    begin
    select @oid=id,@name=name from #temp
    insert into #tb values(@d_id,@name)
    set @nid=@@identity
    exec p_copy @oid,@nid
    delete from #temp where id=@oid
    end
    go--测试
    exec p_copy 2,3--显示处理结果
    select * from #tbgo
    --删除数据测试环境
    drop proc p_copy
    drop table #tb
      

  11.   

    如果照你的要求.我的理解就是下面的例子:/*--复制树形结点 复制指定结点及其下面的子结点--*/
    --创建数据测试环境
    create table #tb(id int identity(1,1),parentid int,name varchar(10))
    insert into #tb
    select 0,'中国'
    union all select 1,'广东'
    union all select 1,'广西'
    union all select 1,'四川'
    union all select 2,'广州'
    union all select 2,'佛山'
    union all select 2,'东莞'
    union all select 5,'越秀区'
    union all select 5,'海珠区'
    union all select 5,'芳村'
    union all select 6,'禅城区'
    union all select 6,'南海区'
    union all select 11,'石湾'go
    --创建复制的存储过程
    create proc p_copy
    @s_id int, --复制该项下的所有子项
    @d_id int, --复制到此项下
    @new_id int --新增加项的开始编号
    as
    declare @nid int,@oid int,@name varchar(10)
    select id,name into #temp from #tb where parentid=@s_id and id<@new_id
    while exists(select 1 from #temp)
    begin
    select @oid=id,@name=name from #temp
    insert into #tb values(@d_id,@name)
    set @nid=@@identity
    exec p_copy @oid,@nid,@new_id
    delete from #temp where id=@oid
    end
    go--创建批量复制的存储过程
    create proc p_copystr
    @s_id varchar(8000) --要复制项的列表,用逗号分隔
    as
    declare @nid int,@oid int,@name varchar(10)
    set @s_id=','+@s_id+','
    select id,name into #temp from #tb
    where charindex(','+cast(id as varchar)+',', @s_id)>0
    while exists(select 1 from #temp)
    begin
    select @oid=id,@name=name from #temp
    insert into #tb values(@oid,@name)
    set @nid=@@identity
    print @oid
    print @nid
    exec p_copy @oid,@nid,@nid
    delete from #temp where id=@oid
    end
    go--测试
    exec p_copystr '5,6'--显示处理结果
    select * from #tbgo
    --删除数据测试环境
    drop proc p_copystr,p_copy
    drop table #tb