j9988 按日期生成自增号!
来源:互联网 发布:阿里云 banner效果 编辑:程序博客网 时间:2024/04/30 09:13
create table num_tb(d datetime,id int)
insert num_tb select '2004-01-01',1
create table tb(id varchar(20),name varchar(10))
create clustered index idx_clu_tb on tb(id)
go
create trigger tri_tb on tb
INSTEAD OF INSERT
as
begin
set nocount on
declare @i int,@id varchar(20),@j int
select @i=count(*) from inserted
begin tran
if exists(select 1 from num_tb where convert(char(8),d,112)=convert(char(8),getdate(),112))
begin
select @j=id from num_tb with(TABLOCKX)
update num_tb with(TABLOCKX) set id=id+@i
end
else
begin
update num_tb with(TABLOCKX) set d=getdate(),id=@i
set @j=0
end
commit tran
select * into #t from inserted
update #t set id=convert(varchar(8),getdate(),112)+right('00000'+rtrim(@j),5),@j=@j+1
insert tb select * from #t
end
go
alter trigger tri_tb on tb
INSTEAD OF INSERT
as
begin
set nocount on
declare @i int,@id varchar(20),@j int
select @i=count(*) from inserted
begin tran
update num_tb with(TABLOCKX) set
id=(case when convert(char(8),d,112)=convert(char(8),getdate(),112)
then id+@i else @i end),
@j=(case when convert(char(8),d,112)=convert(char(8),getdate(),112) then id else 0 end),
d=getdate()
commit tran
select * into #t from inserted
update #t set id=convert(varchar(8),getdate(),112)+right('00000'+rtrim(@j),5),@j=@j+1
insert tb select * from #t
end
go
--创建表
go
--插入记录测试
declare @i int
set @i=0
while @i<=10000
begin
insert into tb(name) values('张三')
insert into tb(name) select '张四'
union all select '张五'
union all select '张六'
union all select '张七'
union all select '张八'
union all select '张九'
union all select '张十'
set @i=@i+1
end
go
--删除环境
drop table tb
- j9988 按日期生成自增号!
- 查表法按日期生成流水号 mssql
- 按日期生成编号
- 按日期生成自动编号
- 如何按日期增量生成文件
- log4j+slf4j日志按日期生成
- log4j 打印sql,按日期生成文件,生成文件位置
- Weblogic日志按日期,每日生成一个新文件
- log4net按日期生成日志文件-C#学习笔记
- mark一下linux按日期生成log文件
- Log4j 重写 RollingFileAppender 实现按日期生成文件夹文件名
- 关于按日期查询
- 按日期分组
- 按日期分组
- 按日期查询记录
- 按日期分组
- log4j 按日期
- mysql 按日期分区
- 深入浅出单例模式
- 搬家……
- 安装 DB2 Version 8.1.6
- 自己看的 perl的环境设置!
- 工作流有点开窍了!
- j9988 按日期生成自增号!
- 放假
- DB2中几种遇到的SQL1032N出错的解决
- 如何保护作为打工者的权利
- 实现ASP页面在做查询时会出现 “正在查询>>>”等提示
- 『JSP学习——全面解析JDBC(1)』
- 『JSP学习——全面解析JDBC(2)』
- COM参数如何传递数组
- 『JSP学习——全面解析JDBC(3)』