表 BListDetail 
(需要将表 AList 的数据转移到这里,条件是 A.AID = AList.AID) 
注意这个表的 BListID 取值 
BListDetailID  BListID Color 
1              11      黑色 
2              11      白色 
3              12      黄色 
4              12      黑色这个位置,有一点错位了,编辑的时候,对得很齐的。

解决方案 »

  1.   

    blistid是自增列?
    insert into blist(bid,title)
    select t1.bid,t2.title
    from a as t1 
    left join b as t2 
    on 1 =1
    BListDetailID自增列
    insert blistdetail(blistid,color)
    select t3.blistid ,t2.color
    from AList as as t2
    left join a as t1 on t1.AID  = t3.AID 
    left join blist as t3 on t1.title = t3.title
      

  2.   

    也可以这样理解:我先用 C# 的语法代替,因为对 SQL 的不太熟如果表A存在记录,则循环插入
    {
       // 开始将表A的记录,插入到表BList
       ...
       
       // 如果表AList存在记录, 条件 A.AID = AList.AID,则循环插入
       {
          // 开始将表AList的记录,插入到表BListDetail,但是BListDetail.BListID 的值,来于表BList.BListID
       }
    }
      

  3.   

    先根据A,B 表写BList 表insert into blist(bid,title)
    select A.bid,B.title
    from A,B
    然后根据 BList表和AList 写BListDetail 表insert blistdetail(blistid,color)
    select c.blistid ,b.color
    from A a,AList b,BList c
    where a.AID =b.AID 
          and a.Title =c.title
      

  4.   


    --> (让你望见影子的墙)生成测试数据,时间:2009-01-18
     
    if not object_id('A') is null
    drop table A
    Go
    Create table A([AID] int,[Title] nvarchar(3))
    Insert A
    select 3,N'名称1' union all
    select 4,N'名称2'
    Go
    Select * from A
    --> (让你望见影子的墙)生成测试数据,时间:2009-01-18
     
    if not object_id('AList') is null
    drop table AList
    Go
    Create table AList([AListID] int,[AID] int,[Color] nvarchar(2))
    Insert AList
    select 1,3,N'黑色' union all
    select 2,3,N'白色' union all
    select 3,4,N'黄色' union all
    select 4,4,N'黑色'
    Go
    Select * from AList
    --> (让你望见影子的墙)生成测试数据,时间:2009-01-18
     
    if not object_id('B') is null
    drop table B
    Go
    Create table B([BID] int,[OrderNumber] nvarchar(4))
    Insert B
    select 8,N'订单号1'
    Go
    Select * from B
    --> (让你望见影子的墙)生成测试数据,时间:2009-01-18
     
    if not object_id('BList') is null
    drop table BList
    Go
    Create table BList([BListID] int,[BID] int,[Title] nvarchar(3))
    Insert BList
    select 11,8,N'名称1' union all
    select 12,8,N'名称2'
    Go
    Select * from BList
    --> (让你望见影子的墙)生成测试数据,时间:2009-01-18
     
    if not object_id('BListDetail') is null
    drop table BListDetail
    Go
    Create table BListDetail([BListDetailID] int, BListID int, Color nvarchar(2))
    Go
    Select * from BListDetailinsert into blistdetail
    select AListID,BListID,Color 
    from a right join alist on a.aid=alist.aid
          join blist on a.Title=blist.titleselect * from  blistdetail
    1 11 黑色
    2 11 白色
    3 12 黄色
    4 12 黑色