create table tb(ID nvarchar(20),col nvarchar(10))
go
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
Create function f_getID()
returns nchar(11)
as
begin
declare @ID int
declare @Nowdate nvarchar(8),@OtherDate nvarchar(8),@Num nchar(13)
select @Nowdate = convert(char(8),getdate(),112),@OtherDate = '19990101',@ID = 0,@Num = ''
select @OtherDate = substring(ID,2,8),@ID = right(ID,2) from tb where substring(ID,2,8) = @Nowdate
if @OtherDate <> @Nowdate
begin
select @ID = 0
end;
select @Num = 'J' + @Nowdate + right((100 + @ID + 1),2)
return @Num
end
insert into tb
select dbo.f_getID(),'aa'
select * from tb
本文介绍了一种使用SQL创建自增序列的方法,通过定义一个存储过程来生成带有日期前缀的唯一标识符。该方法首先检查当前日期是否与已有的记录匹配,如果日期改变则重置序列号为初始值,否则在此基础上递增。
434

被折叠的 条评论
为什么被折叠?



