触发器 实现 自动编号
2009-03-29 10:10
190 查看
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER TRIGGER [Trigger_Insert]
ON [dbo].[表名]
After INSERT
AS
BEGIN
declare @id int,@pid varchar(16),@temppid varchar(16)
--从Inserted表中取得主键的自动编号
select @id=ID from Inserted
--获取当前日期格式为"TJT20081010"
select @pid = 'TJT' + Convert(varchar(8),GetDate(),112);
--获取最后一个PID
select top 1 @temppid=编号 from 表 where 编号 like @pid+'%' order by ID desc
if (@temppid is null)
begin
--如果今天没有插入过数据,则初始值为'TJT200810100001'
set @pid = @pid + '0001'
end
else
begin
--否则从最后一个日期取得编号,并末尾加上1,组成新编号
set @pid = @pid + right(cast(power(10,4) as varchar)+(convert(int,substring(@temppid,12,4))+1),4)
end
--更新编号
update 表 set 编号 =@pid where ID = @id
END
set QUOTED_IDENTIFIER ON
go
ALTER TRIGGER [Trigger_Insert]
ON [dbo].[表名]
After INSERT
AS
BEGIN
declare @id int,@pid varchar(16),@temppid varchar(16)
--从Inserted表中取得主键的自动编号
select @id=ID from Inserted
--获取当前日期格式为"TJT20081010"
select @pid = 'TJT' + Convert(varchar(8),GetDate(),112);
--获取最后一个PID
select top 1 @temppid=编号 from 表 where 编号 like @pid+'%' order by ID desc
if (@temppid is null)
begin
--如果今天没有插入过数据,则初始值为'TJT200810100001'
set @pid = @pid + '0001'
end
else
begin
--否则从最后一个日期取得编号,并末尾加上1,组成新编号
set @pid = @pid + right(cast(power(10,4) as varchar)+(convert(int,substring(@temppid,12,4))+1),4)
end
--更新编号
update 表 set 编号 =@pid where ID = @id
END
相关文章推荐
- 触发器实现自动编号
- 触发器学习(实现自动编号)
- Oracle利用Sequence和触发器Trigger实现Columns的Default自动编号(标识)
- oracle 字段自动编号——触发器实现,非调用序列方法
- sqlserver 触发器学习(实现自动编号)
- Oracle 自动生成编号(实现sqlserver自增长字段)
- SQL语句 实现自动编号功能
- GridView实现自动编号
- Oracle中用触发器实现自动记录表数据被修改的历史信息
- GridView实现自动编号
- php新建文件自动编号的思路与实现
- Excel如何实现表单编号自动递增?
- 发个小程序: 实现"今年流行语大全"的自动编号(今年流行语大全整理之上部分)
- GridView控件自动编号实现
- 在数据库中自动生成编号的实现方法分享
- jquery(javascript)自动序列编号和属性编号实现代码
- ORACLE 中,数据的id号自动生成,利用触发器的实现
- 在Oracle Form中,如何实现自动编号(行号)的功能
- MSCRM二次开发实现自动编号功能