我有两个表tablea和tableb,根据条件将表b中部分数据插入表a,我的XXX代表这个自增序列,
insert into tablea (id, time,name)
select  XXX ,getdate() ,name from tableb T2
where not exists(select 1 from tablea 
where
  tablea.name = T2.name
);
这条语句用触发器实现当插入一行时候XXX自增1?

解决方案 »

  1.   

    insert into tablea (id, time,name)
    select  XXX+1 ,getdate() ,name from tableb T2
    where not exists(select 1 from tablea 
    where
    tablea.name = T2.name
    );
      

  2.   

    http://topic.csdn.net/u/20100124/15/9d154f30-3936-420f-8be8-fe95a9881065.html
      

  3.   

    这里的XXX应该是一个数值吧?
    我使用一个触发器
    create table GlobalSeq 
    (global_id bigint) 
    insert into GlobalSeq values(1) CREATE trigger ko on tablea 
    after insert 
    AS  
    BEGIN  
        declare @cur int    select @cur=max(global_id) from dbo.GlobalSeq
        update temp set tableid=@cur where XXXXX
        set @cur = @cur+1; 
        update GlobalSeq set global_id = @cur 
    end 
    这里的Where条件应该如何写?
      

  4.   

    而且我的tablea 的tableid字段不是设置成identity的
      

  5.   


    insert into tablea (id, time,name) 
    select  id=ROW_NUMBER () OVER(order by XXX)  ,getdate() ,name from tableb T2 
    where not exists(select 1 from tablea 
    where 
    tablea.name = T2.name 
    );
      

  6.   

    to zhangjiang264:
       这个id要实现自增,我的XXX只是一个例子而已