--用自增字段实现难度较大
--建议你把那个字增字段的自增属性去掉,用触发器进行控制
--每次插入记录的话,ID值用程序进行控制
--如果不是批量删除的话,可以这样
create trigger tri1 on tablename
for delete
as
if exists(select 1 from tablename where id>(select id from deleted))
update tablename set id=id-1 where id>(select id from deleted)

解决方案 »

  1.   

    create trigger tri1 on tablename
    for delete
    as
    update tablename set id=id-1 where id>(select id from deleted)
      

  2.   

    该自表里没有update触发器吧?
      

  3.   

    在程序里这样判断,插入数据的时候,取表里的最大ID加1,作为本次插入数据的ID,这样处理应该可以了!
      

  4.   

    一:用自增字段是不能实现的。
    二:用增型字段
      写一函数得到字段值。
    create function getDH() 
    returns int
    As
    begin
        declare @dh1 int
        select @dh1 = max(dh) from  tableName 
        return(IsNull(@dh1,0)+1)
    end/********
       Usage: select dbo.getdh()
    *******/2:在默认值中填入 dbo.getdh()三:
      用触发器来维护
      Create trigger 名 on tableName
      for delete
      As
        Update tableName set id = id- (Select count(*) from deleted)
        where id >= (Select min(id) from deleted)
      

  5.   

    取表里的最大ID加1,作为本次插入数据的ID
    可以吗?
      

  6.   

    SQL还是使用自增字段ID
    建立视图:
    create view xx as
    select (select count(*) from 表 where 自增字段ID<=tem.自增字段ID) 记录号,* from 表 tem
    go这样你看的时候只要看视图就可以了,什么触发器也不要。
      

  7.   


    create proc proc_a
    @n int out
    as
    declare @nn int,@max int
    select @nn=count(自增字段) from tablename
    select @max=max(自增字段) from tablename
    if @nn=@max
    select @n=@nn
    else
    select @n=min(ids)
    from(
    select (select count(*) from tablename where 自增字段<=a.自增字段) ids,* from tablename) a
    where a.ids<>自增字段
    这个存储过程,可以找出自增字段里面最小未用数值
      

  8.   

    大力厉害
    可惜在里面不能排序
    select * from xx order by 记录号 desc降序只能修改成
    create view xx as
    select (select count(*) from 表 where 自增字段ID>=tem.自增字段ID) 记录号,* from 表 tem
    go
      

  9.   

    要真做出来肯定,受不了,当你数据量很大的时候,一个insert让你等待60分钟绝对不是梦。
    如果有10000000条记录,删除id=2的记录要改动多少记录?10000000-1!
    楼主到那时侯就只能节哀,然后改回来了
    奉劝不要动这个脑筋。
      

  10.   

    说错话了是delete 不是insert。
      

  11.   

    编号: 59 发送者 pengdali 发送时间 2003-10-24 16:55:57 删除  回复  
    内容 select (select count(*) from 表 where 自增字段ID<=外面的表.自增字段ID) 记录号,* from 表 外面的表这是利用了子查询的原理,里面的编号小于等于外面的编号。上面“外面的表”是别名,作用是为了区分里面的“表” 和外面的“表”