--用自增字段实现难度较大
--建议你把那个字增字段的自增属性去掉,用触发器进行控制
--每次插入记录的话,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)
--建议你把那个字增字段的自增属性去掉,用触发器进行控制
--每次插入记录的话,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)
for delete
as
update tablename set id=id-1 where id>(select id from deleted)
二:用增型字段
写一函数得到字段值。
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)
可以吗?
建立视图:
create view xx as
select (select count(*) from 表 where 自增字段ID<=tem.自增字段ID) 记录号,* from 表 tem
go这样你看的时候只要看视图就可以了,什么触发器也不要。
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<>自增字段
这个存储过程,可以找出自增字段里面最小未用数值
可惜在里面不能排序
select * from xx order by 记录号 desc降序只能修改成
create view xx as
select (select count(*) from 表 where 自增字段ID>=tem.自增字段ID) 记录号,* from 表 tem
go
如果有10000000条记录,删除id=2的记录要改动多少记录?10000000-1!
楼主到那时侯就只能节哀,然后改回来了
奉劝不要动这个脑筋。
内容 select (select count(*) from 表 where 自增字段ID<=外面的表.自增字段ID) 记录号,* from 表 外面的表这是利用了子查询的原理,里面的编号小于等于外面的编号。上面“外面的表”是别名,作用是为了区分里面的“表” 和外面的“表”