T1              T11ID Name         ID  cID  pID
1   A           1    2    1
2   B----------------------------------
T2              T22ID  Name         ID  cID  pID
10   C           1    10    1
11   D           2    10    2
                 3    11    1有4个表,T1和T11 T2和T22 
条件:
1,T1和T2的结构完全一样,T11和T22的结构完全一样
2,四个表中的ID字段均为自增长字段(+1)目的:
1,把T2表中ID=10的记录插入到T1表中,(T1表新增加了一条记录的同时,这条记录会有一个新的ID,例如3)
2,同时,把T22表中cID=10的记录插入到T11表中,重要的是:把cID的值换成刚才T1表中新增那条记录的ID,例如cID=3(当然,不要修改T22表)(简单地说,就是cID会随着T1表中的ID值改变)
3,删除T2表和T22表中所有被(成功)移动的记录结果如下:
T1              T11ID Name         ID  cID  pID
1   A           1    2    1
2   B           2    3    1
3   C           3    3    2----------------------------------
T2              T22ID  Name         ID  cID  pID          
11   D           3    11    1不知道说明白没有,分不多了,就给20分吧,见谅

解决方案 »

  1.   

    T1              T11 ID Name        ID  cID  pID 
    1  A           1    2    1 
    2  B ---------------------------------- 
    T2              T22 ID  Name        ID  cID  pID 
    10  C            1    10    1 
    11  D            2    10    2 
                     3    11    1 --上面的两个表,操作后变成如下的结果:T1              T11 ID Name        ID  cID  pID 
    1  A            1    2    1 
    2  B            2    3    1 
    3  C            3    3    2 ---------------------------------- 
    T2              T22 ID  Name        ID  cID  pID          
    11  D            3    11    1 
    用存储过程实现也可以
      

  2.   


    T1                  T11 ID Name             ID  cID  pID 
    1  A                1    2    1 
    2  B ---------------------------------- 
    T2                 T22 ID  Name           ID   cID   pID 
    10  C               1    10    1 
    11  D               2    10    2 
                        3    11    1 --上面的四个表,操作后变成如下的结果: T1                  T11 ID Name            ID  cID  pID 
    1  A                1    2    1 
    2  B                2    3    1 
    3  C                3    3    2 ---------------------------------- 
    T2                  T22 ID  Name            ID  cID  pID          
    11  D                3    11    1 
    --用存储过程实现也可以
      

  3.   


    --突然发现
    //CSDN的这个功能
    <!--挺不错的啊!-->
      

  4.   


    T1                  T11                              T1                    T11ID Name             ID  cID  pID                     ID Name               ID  cID  pID 
    1  A                1    2    1                      1  A                   1    2    1 
    2  B                                                 2  B                   2    3    1      --这两条记录是从T22表中移动过来的
                                                         3  C  --这条记录        3    3    2      --但是注意 cID已经由10变为了3
                                                                       --是在T2中移动                                    
                                                               --过来的
                                                                       --但是注意 ID已经由10变为了3
    ----------------------------------     结果---->>    ----------------------------------
    T2                 T22                               T2                    T22 ID  Name           ID   cID   pID                    ID  Name              ID  cID  pID    
    10  C               1    10    1                     11  D                  3    11    1 
    11  D               2    10    2 
                        3    11    1 --其实主要的技术问题就有两个
    --1,T2表中ID=10的记录插入到T1表中后肯定会生成一个新的ID,关键是如何取出这个新ID
    --2,T22表中ID=10的记录插入到T11表的时候,不能使用原来的cID,而要使用步骤1中的新ID值替换cID/*
    可以使用存储过程来实现
    */
      

  5.   


    <!--各位老大,我形容的还不够清楚啊,你们不是万圣节拿我开涮吧?-->
      

  6.   


    rem 尤其是上面两位猩猩
    '太不厚道了[code=BatchFile]
    rem 人家穿裤衩的都看懂了
    [/code]
      

  7.   

    对 t1 建一个 insert 触发器 即可 
    然后你只需要插入你想插入的姓名即可 自动完成,不只局限于你原来所说的id = 10
    create trigger  del_t2
    on t1
    for  insert 
    as
    declare @cid int 
    set @cid = (select id from t2 where name in (select name from inserted ))
    insert into t11 
    select (select id from inserted)as cid ,pid from t22 where cid in 
    (select id from t2 where name in (select name from inserted ))
    delete from t2 where id = @cid
    delete from t22 where cid =@cid
      

  8.   

    create table #T1
    (
    ID int identity,
    Name nvarchar(20)
    )create table #T11
    (
    ID int identity,
    cID int,
    pID int
    )create table #T2
    (
    ID int ,
    Name nvarchar(20)
    )create table #T22
    (
    ID int identity,
    cID int,
    pID int
    )insert into #T1(Name)
    select 'A'
    union all
    select 'B'insert into #T11(cID,pID)
    select 2,1insert into #T2(ID,Name)
    select 10,'C'
    union all
    select 11,'D'insert into #T22(cID,pID)
    select 10,1
    union all
    select 10,2
    union all
    select 11,1create proc test
    @ID int
    as
    begin
    begin try
    begin tran
    declare @identityID int
    insert into #T1(Name) select Name from #T2 where ID=@ID
    set @identityID=@@identity
    insert into #T11(cID,pID)
    select @identityID,pID from #T22 where cID=@IDdelete from #T2 where ID=@ID
    delete from #T22 where cID=@ID
    commit tran
    end try
    begin catch
    rollback tran
    end catch
    endexec test 10
    完成后删除测试用表和存储过程
    drop table #T1drop table  #T11drop table  #T2
    drop table  #T22
    drop proc test