主表:
T1:主键C0自动增1
内容:
C0 Name Sex
1 王  男
2 李  女从表:主键自动增1
T2:
Id C0 work_n work_d要求在从表中添加每一主表人员一组从表数据如下
work_n work_d
办公  200601
财务  200602
汽车  200504

解决方案 »

  1.   

    进行插入操作时使用存储过程:insert into T1(Name,Sex) values(@name,@sex)
    if(@@error=0)
    begin
      set @returnid=SCOPE_IDENTITY( )
      insert into T2(C0,work_n,work_d) select @returnid,'办公',200601
        union all select @returnid,'财务',200602
        union all select @returnid,'汽车',200604
    end
      

  2.   

    谢谢楼上的朋友,但我不是这个意思,我是想对主表T1中已知记录中逐一得到C0键值,再在从表T2中添加已知键值的一组数据,最后得到T2中的数据结果如下:Id C0 work_n work_d
     1   1  办公  200601
     2   1  财务  200602
     3   1  汽车  200504
     4   2  办公  200601
     5   2  财务  200602
     6   2  汽车  200504
      

  3.   

    只须建一个#t1的临时表,与主表做交叉联接,然后把数据插入从表create table #t(id int,name varchar(10))
    insert into #t select 1,'王'
    union all select 2,'李'select * from #tcreate table #t1(id int,work_n varchar(10),work_d int)
    insert into #t1 select 1,'办公',200601
    union all select 2,'财务',200602
    union all select 3,'汽车',200604create table #t2(id int identity,C0 int,work_n varchar(10),work_d int)insert into #t2(C0,work_n,work_d) select a.id,b.work_n,b.work_d from #t a,#t1 b order by a.idselect * from #t2