我先描述一下:
现在有两张空表A,B
A表有一个字段number (int)
B表有四个字段C1,C2,C3,C4 (int)问题:
第一次我把一个数据(比如10)插入到A表number中,同时也插到B表C1中
第二次我又把一个数据(比如13)插入到A表number中,同时也插到B表C2中
第三次...................18................................C3..
第四次...................26................................C4..
每次A表number中新增一个数都依次插入B表各个字段中
四次过后的结果如下:
A:  number                     B: C1  C2  C3  C4
     10                           10  13  18  26
     13
     18
     26请问怎样才能实现?

解决方案 »

  1.   

    晕啊`~我是用JAVA的JDBC来操作数据库
    看来只有到程序端去实现了
      

  2.   

    create table ta(number int)create trigger test_tr on ta
    for insert
    as
    begin
    DECLARE @number int,@sql varchar(4000),@sql1 varchar(4000),@i int
    DECLARE roy CURSOR
    FOR SELECT * from inserted
    OPEN roy
    FETCH next FROM roy into @number
    WHILE @@FETCH_STATUS = 0
    begin
    begin
      if exists(select 1 from sysobjects where name='tb' and xtype='U')
        begin
             select @i=max(colid)+1 from syscolumns where id=object_id('tb')
             set @sql=N'alter table tb add C'+cast(@i as varchar)+' int null'
             set @sql1=N'update tb set C'+cast(@i as varchar)+'='+cast(@number as varchar)
             exec(@sql)
             exec(@sql1)
        end
    else
        begin
             set @sql=N'create table tb(C1 int)'
             set @sql1=N' insert tb select C1='+cast(@number as varchar)
             exec(@sql)
             exec(@sql1)
        end
    end
    FETCH NEXT FROM roy INTO @number
    end
    CLOSE roy
    DEALLOCATE roy
    end
    测试:
    insert ta 
    select 10 union all
    select 13 union all
    select 18 union all
    select 26
    查询:
    select * from tb
    结果为:
    C1          C2          C3          C4          
    ----------- ----------- ----------- ----------- 
    10          13          18          26(所影响的行数为 1 行)