这个不是触发器,但是能实现你的需求:
--自已做标识列的例子:--创建得到最大id的函数
create function f_getid()
returns int
as
begin
declare @id int
select @id=max(id) from tb
set @id=isnull(@id,0)+1
return(@id)
end
go--创建表
create table tb(id int default dbo.f_getid(),name varchar(10))
go--创建触发器,在删除表中的记录时,自动更新记录的id
create trigger t_delete on tb
AFTER delete
as
declare @id int,@mid int
select @mid=min(id),@id=@mid-1 from deleted
update tb set id=@id,@id=@id+1 where id>@mid
go--插入记录测试
insert into tb(name) values('张三')
insert into tb(name) values('张四')
insert into tb(name) values('张五')
insert into tb(name) values('张六')
insert into tb(name) values('张七')
insert into tb(name) values('张八')
insert into tb(name) values('张九')
insert into tb(name) values('张十')--显示插入的结果
select * from tb--删除部分记录
delete from tb where name in('张五','张七','张八','张十')--显示删除后的结果
select * from tb--删除环境
drop table tb
drop function f_getid
--自已做标识列的例子:--创建得到最大id的函数
create function f_getid()
returns int
as
begin
declare @id int
select @id=max(id) from tb
set @id=isnull(@id,0)+1
return(@id)
end
go--创建表
create table tb(id int default dbo.f_getid(),name varchar(10))
go--创建触发器,在删除表中的记录时,自动更新记录的id
create trigger t_delete on tb
AFTER delete
as
declare @id int,@mid int
select @mid=min(id),@id=@mid-1 from deleted
update tb set id=@id,@id=@id+1 where id>@mid
go--插入记录测试
insert into tb(name) values('张三')
insert into tb(name) values('张四')
insert into tb(name) values('张五')
insert into tb(name) values('张六')
insert into tb(name) values('张七')
insert into tb(name) values('张八')
insert into tb(name) values('张九')
insert into tb(name) values('张十')--显示插入的结果
select * from tb--删除部分记录
delete from tb where name in('张五','张七','张八','张十')--显示删除后的结果
select * from tb--删除环境
drop table tb
drop function f_getid
create function f_getid()
returns char(3)
as
begin
declare @id intif not exists(select 1 from tb where id='001')
set @id=1
else
begin
select @id=max(id) from tb
if @id is null
set @id=1
else
begin
declare @id1 int
select @id1=min(id) from tb a where id<>@id and not exists(select 1 from tb where id=a.id+1)
if @id1 is not null set @id=@id1
set @id=@id+1
end
endlb_re:
return(right('000'+cast(@id as varchar),3))
end
go--创建表
create table tb(id char(3) default dbo.f_getid(),name varchar(10))
go
--插入记录测试
insert into tb(name) values('张三')
insert into tb(name) values('张四')
insert into tb(name) values('张五')
insert into tb(name) values('张六')
insert into tb(name) values('张七')
insert into tb(name) values('张八')
insert into tb(name) values('张九')
insert into tb(name) values('张十')--显示插入的结果
select * from tb--删除部分记录
delete from tb where name in('张三','张七','张八','张十')--显示删除后的结果
select * from tb--再次插入记录
insert into tb(name) values('李一')
insert into tb(name) values('李二')--显示插入的结果
select * from tb order by idgo
--删除环境
drop table tb
drop function f_getid
id name
---- ----------
001 张三
002 张四
003 张五
004 张六
005 张七
006 张八
007 张九
008 张十(所影响的行数为 8 行)
(所影响的行数为 4 行)id name
---- ----------
002 张四
003 张五
004 张六
007 张九(所影响的行数为 4 行)
(所影响的行数为 1 行)
(所影响的行数为 1 行)id name
---- ----------
001 李一
002 张四
003 张五
004 张六
005 李二
007 张九(所影响的行数为 6 行)
create function f_getid(@type char(2))
returns char(6)
as
begin
declare @re char(6)
if exists(select 1 from 表 where type=@type and 编号=@type+'-001')
select @re=@type+'-'+right('000'+cast(cast(right(id,3) as int)+1 as varchar),3)
from(
select id=min(编号) from 表 a
where type=@type and not exists(
select 1 from 表 where type=@type and 编号=@type+'-'
+right('000'+cast(cast(right(a.编号,3) as int)+1 as varchar),3)
)) a
return(isnull(@re,@type+'-001'))
end
go--创建测试表
create table 表(type char(2),编号 char(6) primary key default '',名称 varchar(10))
go--创建触发器,自动生成项目编号
create trigger t_insert on 表
instead of insert
as
declare @type char(2),@名称 varchar(10)
declare tb cursor local for select type,名称 from inserted
open tb
fetch next from tb into @type,@名称
while @@fetch_status=0
begin
insert 表(type,编号,名称)
select @type,dbo.f_getid(@type),@名称
fetch next from tb into @type,@名称
end
close tb
deallocate tb
go
--插入数据测试
insert into 表(type,名称)
select '收','aa'
union all select '付','bb'
union all select '付','ab'
union all select '收','bg'
union all select '收','hh'--显示结果
select * from 表
go--删除部分记录
delete from 表 where 名称 like 'b%'--显示结果
select * from 表
go--再次插入数据测试
insert into 表(type,名称)
select '收','aa1'
union all select '付','bb2'
union all select '付','ab3'
union all select '收','bg4'
union all select '收','hh5'--显示结果
select * from 表
go
--删除测试环境
drop table 表
drop function f_getid/*--测试结果--插入时,自动编号的效果type 编号 名称
---- ------ ----------
付 付-001 bb
付 付-002 ab
收 收-001 aa
收 收-002 bg
收 收-003 hh(所影响的行数为 5 行)
--删除部门记录的结果type 编号 名称
---- ------ ----------
付 付-002 ab
收 收-001 aa
收 收-003 hh(所影响的行数为 3 行)
--再次插入的处理结果
type 编号 名称
---- ------ ----------
付 付-001 bb2
付 付-002 ab
付 付-003 ab3
收 收-001 aa
收 收-002 aa1
收 收-003 hh
收 收-004 bg4
收 收-005 hh5(所影响的行数为 8 行)
--*/