如有一张表记录
id     name
 1      张三
 2      李四
当我添加了一条记录后(值为“王五”),如何通过触发器,让这个王五记录行的id变为3(即最大流水号)?

解决方案 »

  1.   

    你直接设计表的时候用identity不行了吗
    你说的这个,也可以实现,但有很多种情况。你如果一次插入一条数据,用下面的这个触发器就行。
    create table jx(id int,name nvarchar(10))insert jx select 1,N'张三'
    insert jx select 2,N'李四'
    --创建触发器
    create trigger t_ins on jx for insert
    as
    update jx set id=(select max(id) from jx)+1 where id is null--插入测试数据
    insert jx(name) select N'礼拜'